简体   繁体   English

每行打印10个数字

[英]Print 10 Numbers Per Line

I am trying to print a loop of numbers, with 10 numbers per line. 我正在尝试打印一个数字循环,每行10个数字。 Here is what I have done so far: 这是我到目前为止所做的:

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. 我已经尝试创建另一个变量j <11的循环,然后放入System.out.println()然后只打印10返回然后下一个数字。 I am trying to accomplish this output: 我想完成这个输出:

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

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

Here is demo 这是演示

I can't understand how can you get that output because you are incrementing i in for loop and inner body of loop also 我无法理解你怎么能得到那个输出,因为你也在循环和循环的内部体积递增i

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

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

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

Try this 尝试这个

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

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

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

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