java习题 用Math中的random方法, 随机输出5个1~6的整数代码。

2025-02-24 13:56:23
推荐回答(4个)
回答1:

(int) (Math.random() * 100) % 6 + 1

回答2:

(int)(Math.random()*5+1;

回答3:

public class Test{
public static void main(String[] args){
for(int i=0; i<5; i++){
int x = (int)(Math.random()*6)+1;
System.out.println(x);
}
}
}

回答4:

public class RandomOut {
public static void main(String [] args){
for(int i=0;i<5;i++){
int tmp=(int)(Math.random()*6)+1;
System.out.println(tmp);
}
}
}

※ 0<=Math.random()<1