简体   繁体   English

Java程序按顺序打印数字

[英]Java Program printing numbers in sequence

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øvregrense: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

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 1.确定在一行中打印多少个数字
2. Actually print the numbers 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 loop does 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 ? 够容易吗?

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循环?

Keep track of the line number eg 跟踪行号,例如

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++;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM