编写一JAVA程序,随机产生20个0-100之间的正整数,按每行5个数输出

2024-11-22 22:23:47
推荐回答(2个)
回答1:

Random random = new Random();
for(int i = 0; i < 20; i++) {
System.out.print(random.nextInt(100));
if((i + 1) % 5 == 0) {
System.out.println();
} else {
System.out.print(",");
}
}

回答2:

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "";
for(int i = 1 ;i < 21 ;i++){
str = str+" "+Math.round(Math.random()*100);
if(i % 5 == 0 ){
System.out.println(str);
str = "";
}
}
}

}