Java 每行打印 10 个数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22680383/
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-13 17:15:05  来源:igfitidea点击:

Print 10 Numbers Per Line

java

提问by Sameer A.

I am trying to print a loop of numbers, with 10 numbers per line. Here is what I have done so far:

我正在尝试打印一个数字循环,每行 10 个数字。这是我到目前为止所做的:

for(int i = 100; i < 200; i++) {

    System.out.print(i++);

    System.out.print(" ");

}

The output I get is

我得到的输出是

100 101 102 103 104 105 106 107 108 109 110 111....

I have tried creating another loop with variable j < 11 and then putting System.out.println() however that just prints 10 returns then the next number. I am trying to accomplish this output:

我试过用变量 j < 11 创建另一个循环,然后放入 System.out.println() 但是它只打印 10 返回然后下一个数字。我正在尝试完成此输出:

100 101 102 103 104 105 106 107 108 109
110 111 112 113...

采纳答案by Zaheer Ahmed

try ternary operator?:

尝试三元运算符?:

If your input is fixed from 100 to 200 then you can try:

如果您的输入固定在 100 到 200 之间,那么您可以尝试:

for(int i = 100; i < 200; i++) {    
     System.out.print(i+ ((i%10==9) ? "\n" : " "));
}

will output:

将输出:

100 101 102 103 104 105 106 107 108 109
110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129
130 131 132 133 134 135 136 137 138 139
...

100 101 102 103 104 105 106 107 108 109
110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129
130 131 132 133 134 135 136 137 138 139
... ...

Here is Demo on IDEONE

这是IDEONE上的演示

But if you input is not fixed then try ie 105 to 200:

但是如果你的输入不是固定的,那么尝试即 105 到 200:

int start = 105;
for(int i = start; i < 200; i++) {
  System.out.print(i+ ((i-(start-1))%10==0 ? "\n" : " "));
}

will output:

将输出:

105 106 107 108 109 110 111 112 113 114
115 116 117 118 119 120 121 122 123 124
125 126 127 128 129 130 131 132 133 134
...

105 106 107 108 109 110 111 112 113 114
115 116 117 118 119 120 121 122 123 124
125 126 127 128 129 130 131 132 133 134
... ...

Here is demo

这是演示

回答by Naveen Ramawat

Try this

尝试这个

    for(int i = 100; i < 200; i++) {
            System.out.print(i+ "  ");    
            if(i%10 == 9)
               System.out.println();    
    }

回答by Ramanqul Buzaubak

    for(int i = 100; i < 200; i++) {
        if (i>100 && i % 10 == 0) {
              System.out.println();
        }
        System.out.print(i);
        System.out.print(" ");
    }

回答by brijesh k

try this

尝试这个

      int counter=1;
        for(int i = 100; i < 200; i++) {
             if(counter==10)
    {   
counter=1;
 System.out.println("\n");
    }
    else
    {
            System.out.print(i++ + "  "); 
            counter++;   
     }   

            System.out.println("");    
        }

回答by JavaLearner

I can't understand how can you get that output because you are incrementing i in for loop and inner body of loop also

我不明白你怎么能得到那个输出,因为你也在 for 循环和内部循环体中增加 i

for(int i = 100; i < 200; i++) {

    System.out.print(i+" ");
    if(i%10==0)
        System.out.print("\n");
}

回答by Pratik Roy

for(int i = 100; i < 200; i++) {
    if(i%10==0){
        System.out.println();
    }
    System.out.print(i+" ");
}

回答by user3467480

for (int i = 100; i < 200; i++) {

        if (i != 100 && i % 10 == 0) {
            System.out.println("");
        }
        System.out.print(i + " ");
    }