简体   繁体   English

(Java数字金字塔)如何使我的数字在它们之间具有更多的空格,同时当它是大于9的整数时准确对齐吗?

[英](Java Number Pyramid) How do I get my numbers to have more spaces between them while lining up accurately when it is an integer is > 9?

I am having difficulty getting the desired output. 我很难获得所需的输出。 I know there are problems that are similar to mine that is already posted, but I find it hard to relate my code to their solutions without a massive overhaul. 我知道有些问题已经和我已经发布的问题类似,但是我发现很难在不进行大修的情况下将我的代码与其解决方案联系起来。

The solution for my class assignment: 我的课堂作业的解决方案:

Supposed to continue until every direction of pyramid equals 1 My second method "spaces" is redundant and I am not sure why. 应该继续下去,直到金字塔的每个方向都等于1。我的第二种方法“空格”是多余的,我不确定为什么。 Any help would be appreciated. 任何帮助,将不胜感激。

Blockq Blockq

    public static void main(String[] args) {
        numPar();
        spaces();
    }

    private static void spaces() {
        int x = 0;
        if(x > 0 && x < 10) {
            System.out.print("   "); 
        } else if (x > 10 && x < 99) {
            System.out.print("  ");  
        } else if (x > 99) {
            System.out.print(" ");  
        }
    }

    private static void numPar() {
        int spaces = 14;
        for(int i = 0; i<= 7; i++) {
            for(int u = 0; u<spaces; u++) {
                System.out.print(" ");
            }
            spaces--;
            spaces--;
            for(int j = 0 ; j <i ; j++) {
                System.out.print(""+ (int) Math.pow(2,j)+" ");
            }
            for(int k = i ; k >=0 ; k--) {
                System.out.print(""+ (int) Math.pow(2,k)+" ");
            }
            System.out.println("");
        }
    }
}

I made every number take 3 places using String.format("%3s", (int) Math.pow(2, j)) . 我使用String.format("%3s", (int) Math.pow(2, j))使每个数字占据3位。 You can make it dynamic by replacing the number 3 here with the length of the largest number you'll print. 您可以通过将此处的数字3替换为要打印的最大数字的长度来使其动态化。 I also changed the number of spaces in your print statements. 我还更改了打印语句中的空格数。 Here is the full code that prints an evenly spaced pyramid:- 这是打印均匀分布的金字塔的完整代码:-

public static void main(String[] args) {
    numPar();
    spaces();
}

private static void spaces() {
    int x = 0;
    if (x > 0 && x < 10) {
        System.out.print("   ");
    } else if (x > 10 && x < 99) {
        System.out.print("   ");
    } else if (x > 99) {
        System.out.print("   ");
    }
}

private static void numPar() {
    int spaces = 14;
    for (int i = 0; i <= 7; i++) {
        for (int u = 0; u < spaces; u++) {
            System.out.print("  ");
        }
        spaces--;
        spaces--;
        for (int j = 0; j < i; j++) {
            System.out.print("" + String.format("%3s", (int) Math.pow(2, j)) + " ");
        }
        for (int k = i; k >= 0; k--) {
            System.out.print("" + String.format("%3s", (int) Math.pow(2, k)) + " ");
        }
        System.out.println("");
    }
}

String.format explanation :- String.format解释 :-
String.format("%3s", str) will print the string str , padding it with spaces, to make the total length 3 if it's less than 3. Note that you can write anything instead of 3 - I used 3 because your biggest number was of length 3. String.format("%3s", str)将打印字符串str ,并用空格填充,以使总长度3(如果小于3的话)。请注意,您可以写任何东西来代替3-我使用3是因为您最大数字长度为3。

So "A" will be printed as "_ _ A" (2 spaces), and "Ab" will be printed as "_ Ab" (1 space). 因此,“ A”将被打印为“ _ _ A”(2个空格),而“ Ab”将被打印为“ _ Ab”(1个空格)。

I just replaced str with your Math.pow(2, j) . 我只是用您的Math.pow(2, j)替换了str

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Java中的字符串中有多个空格,那么如何将它们压缩到单词之间的单个空格中? - If you have multiple spaces inside a string in Java, how do you condense them into a single space between words? 我如何递归地做我的金字塔数字模式? - How do I do my Pyramid number pattern recursively? java-如何计算段落之间的空白并从总行数中减去它们? - java- How do I count the empty spaces between paragraphs and subtract them from the total line count? 我如何获得最小数量的ArrayList <Integer> 需要具有从1到“ n”的所有数字吗? - how I can get the minimum number of ArrayList<Integer> needed to have all numbers from 1 to “n”? Java:我如何让我的程序确定某个数字是否增加了一定数量,以及是否确定了一个特定的方程式? - Java: How do I have my program determine if a number has went up a certain amount and if so do a particular equation? 如何在Java中的字符串中的空格之间导出数字? - How can I export numbers between spaces in a string in Java? JAVA:如何解析文本文件行中的整数(由可变数量的空格分隔) - JAVA: How to parse Integer numbers (delimited by a variable number of spaces) in a line of a text file 如何添加更多球并使它们具有不同的起始位置? 我的aproach甚至可能吗? - How do I add more balls and make them have a different starting position? Is that even possible with my aproach? 如何在 Java 中打印这个特定的金字塔? - How do I print this specific pyramid in Java? 如何在Android中将小空格作为数字分隔符? - How do I get tiny spaces as separator of numbers in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM