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

如题所述

第1个回答  2012-08-08
(int) (Math.random() * 100) % 6 + 1
第2个回答  2012-08-08
(int)(Math.random()*5+1;追问

能帮我吧代码具体实现以下吗?
万分感谢!!!

追答

for(i = 0, i<5,i++){
int tmp = (int)(Math.random()*5)+1;

System.out.println(tmp)
}

追问

我想要的是输出5个整数,范围是在1~6的啊?这个怎么都是输出1呢?

追答

把1 放到括号外边试一下

追问

 

追答

import java.math.*;

public class test

{

    public static void main(String[] args)

    {

        for(int i =1; i<5; i++){

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

            System.out.println(tmp);

        }

    }

}


第3个回答  2012-08-08
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个回答  推荐于2016-03-26
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本回答被提问者和网友采纳