简体   繁体   English

如何每行显示10个数字? -初学者-

[英]How to display 10 numbers per line? -Beginner-

I am currently working on a program involving arrays and I have generated 200 random numbers ranging between 0-100. 我目前正在开发一个涉及数组的程序,并且已经生成了200个随机数,范围在0-100之间。 But I cannot display all the numbers, 10 per line. 但是我无法显示所有数字,每行10个。 This is my current code: 这是我当前的代码:

import java.util.Random;

public class RandomStats {

    public static void main(String[] args) {
        Random random = new Random ();

        for(int i=0; i<200; i++){
            int randomNumber = random.nextInt(100);
            System.out.println("Generated 200 random numbers: " + randomNumber);
        }       
    }
}
  • System.out.println() prints what you tell it, and moves to the next line. System.out.println()打印您所告诉的内容,然后移至下一行。
  • System.out.print() , however, prints stuff and stays on the same line. 但是, System.out.print()打印内容并保持在同一行。 Next call to System.out.print() will continue on the same line. System.out.print()下一次调用将在同一行上继续。

Play with that. 玩那个。

you basically need two loops, the outside loop goes through [0-200), and the internal loop prints out 10 numbers per line: 您基本上需要两个循环,外部循环经过[0-200),内部循环每行输出10个数字:

for (int i = 0; i < 200; i++)
{
   for (int j = 0; j < 10; j++)
   {
     System.out.print(randomNumber + " ");
   }
   System.out.print("\n");  // change to a new line
}

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

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