我需要帮助使用java编写一个程序,该程序允许您用textField编写一个数字,然后使用i= (int) (Math.random() * 10.0)从0-9生成该数量的随机数。例如,如果我在textField中编写5,程序将从0-9生成5个随机数。
谢谢
发布于 2016-04-10 19:03:42
好的,因为您希望使用Math.random()方法,请尝试以下操作:
int times = 5;//how many numbers to output
for(int i = 0; i < times; i++)
{
System.out.println((int)(Math.random()*10));
//you must cast the output of Math.random() to int since it returns double values
}我乘以10,因为Math.random()返回大于或等于0.0且小于1.0的值。
发布于 2016-04-11 06:41:41
使用新的Java 8流API:
int n = Integer.parseInt(myTextField.getText());
int[] random = ThreadLocalRandom.current().ints(0, 10).limit(n).toArray();Random实例)发布于 2016-04-10 18:44:14
int x = value entered in textfield;
int generatedRandomNumber = 0;
java.util.Random rand = new java.util.Random();
for(int i=0 ; i<x ; i++) {
generatedRandomNumber=rand.nextInt(10);//here you have your random number, do whatever you want....
}https://stackoverflow.com/questions/36533823
复制相似问题