Java程序按顺序打印数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19498131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:46:21  来源:igfitidea点击:

Java Program printing numbers in sequence

java

提问by user2903527

I'm writing this program with numbers, but I am stuck and need some help.

我正在用数字编写这个程序,但我被卡住了,需要一些帮助。

Code so far:

到目前为止的代码:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Oppgi ?vre grense: ");
    int Number = in.nextInt(); 
    int tall = 1;
    for(int t = 0; tall <=45; tall++){
            System.out.println(" " + tall);
    }   
}

The objective: to get the first line to contain one number, the second to contain two numbers, third line contain three numbers, etc. The output should look like a pyramid, with different spacing between the numbers on each line.

目标:让第一行包含一个数字,第二行包含两个数字,第三行包含三个数字,等等。输出应该看起来像一个金字塔,每行数字之间的间距不同。

If anybody can help me with the solution code. Thank you.

如果有人可以帮助我解决解决方案代码。谢谢你。

Oppgi ?vre grense: 45 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

oppgi ?vre grense: 45 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 4 3 4 3 4 3 4 3 3 4 3 3 4 3 3 16 45

回答by Little Child

outer loop{
    decides number of numbers in one line
    inner loop{
        prints actual numbers.
        Please keep track of the numbers you have printed so far
        Inner loop starts at numbers printed so far
        It will have passes = number of numbers to print
    }
}  

You have two distinct tasks here:
1. Decide how many numbers to print in one line
2. Actually print the numbers

您在这里有两个不同的任务:
1. 决定在一行中打印多少个数字
2. 实际打印数字

Since that is the case, one loop decides how many numbers to print: the outer loop. The Reason it is outer loop is because you need to have a clear picture of how many numbers you need to print before you actually print.
The other loop: inner loopdoes the actual printing.

既然如此,一个循环决定打印多少个数字:外循环。它是外循环的原因是因为您需要在实际打印之前清楚地了解需要打印多少个数字。
另一个循环:内部循环进行实际打印。

So, once you start with the outer loop, your inner loop will begin printing.
It will then see if it has printed the maximum number of numbers for that pass.
If yes, stop. Then, you increment the outer loop. Come back in, print, check and then do the same.

因此,一旦您从外循环开始,您的内循环将开始打印。
然后它将查看它是否已为该传递打印了最大数量的数字。
如果是,停止。然后,您增加外循环。回来,打印,检查,然后做同样的事情。

Easy enough ?

够容易吗?

回答by JoeC

Keep track of the line number e.g.

跟踪行号,例如

int line = 1; // line number
int count = 0; // number of numbers on the line
for(int x = 0; x <= 45; x++){
    if (count == line){
        System.out.println(""); // move to a new line
        count = 0; // set count back to 0
        line++; // increment the line number by 1
    }
    System.out.print(x); // keep on printing on the same line
    System.out.print(" "); // add a space after you printed your number
    count++;
}

回答by user3284142

public class RareMile {

    public static void main (String[] args){
        printNum(5);
    }

    public static void printNum (int n){
        int k=1, sum=0;
        for (int i=1; i<=n; i++){
            sum=0;
            for(int j=1; j<=i; j++){    
                System.out.print(k);
                sum = sum+k;
                k++;                
                }
            System.out.print("            =" + sum);
            System.out.println();
        }        
    }

}

The real question is that how to do it with only one for loop?

真正的问题是如何只用一个 for 循环来做到这一点?