Java 需要帮助根据用户输入随机化数字吗
//获取允许随机化的数字的范围Java 需要帮助根据用户输入随机化数字吗,java,Java,//获取允许随机化的数字的范围 public class numberCube { public static int size; public static int tosses; public static int random; public static int value; public static int values; public static void cubeSize(){ //获取随机化器将循环的次数 Str
public class numberCube {
public static int size;
public static int tosses;
public static int random;
public static int value;
public static int values;
public static void cubeSize(){
//获取随机化器将循环的次数
String x = JOptionPane.showInputDialog
("How many numbers do you want on your cube?");
int size = Integer.parseInt(x);
}
public static void numTosses(){
//创建随机数。这就是问题所在。我需要能够//允许用户指定数字的范围以及它将//随机化的次数
例如,对于cubeSize()我可以输入3,对于numTosses()我可以输入5。可能的输出为:1,1,3,2,3
String y = JOptionPane.showInputDialog
("How many times do you want to toss the dice?");
int tosses = Integer.parseInt(y);
}
public static void randomizer(){
您正在复制变量的初始化
(例如:大小、投掷等)
如果要操作特定类的属性,需要使用this
关键字。所以
int size=Integer.parseInt(x)
变成this.size=Integer.parseInt(x)代码>
int-hows=Integer.parseInt(y)
变成this.tows=Integer.parseInt(y)代码>
etc..
然后在randomizer()
方法中,您可以尝试以下操作:
}
}
}
Random Random=new Random();
对于(inti=1;i您正在复制变量的初始化(例如:大小、投掷等)
如果要操作特定类的属性,需要使用this
关键字。因此
int size=Integer.parseInt(x);
变成this.size=Integer.parseInt(x);
int-hows=Integer.parseInt(y);
变成this.hows=Integer.parseInt(y);
etc..
然后在randomizer()
方法中,您可以尝试以下操作:
}
}
}
Random Random=new Random();
对于(inti=1;i你的意思是这样的
Random random = new Random();
for (int i = 1; i <= this.tosses; i++) {
int value = 1 + random.nextInt(this.size);
System.out.println(value);
}
Random r=new Random();
对于(int i=0;i
你的意思是这样的
Random random = new Random();
for (int i = 1; i <= this.tosses; i++) {
int value = 1 + random.nextInt(this.size);
System.out.println(value);
}
Random r=new Random();
对于(int i=0;i 问题是什么?我想知道int value=16+random.nextInt(5);
能做什么,你期望它能做什么。据我所知,它会导致16+一些0到4的随机数。所以,它会给出16到20的数字。我的错,那代码是从谷歌的某个地方复制的。整个随机发生器()方法可以更改,但它最好能工作,因为我不知道从哪里开始。嗯..问题是什么?我想知道int value=16+random.nextInt(5)
可以,你希望它能做什么据我所知,它会产生16+一些从0到4的随机数。所以,它会给出大约16到20的数字。我的错,那代码是从google上的某个地方复制的。整个随机化器()方法可以更改,但它最有效,因为我不知道从哪里开始。是的,这正是我想要的,但当我使用变量而不是实际数字时,它似乎没有输出任何东西。是的,这正是我想要的,但当我使用变量而不是实际数字时,它似乎没有输出任何东西。Th谢谢你!工作得很好。np!如果它有帮助,不要忘记接受它,因为它可能对将来的其他人有用!快乐编码…非常感谢!工作得很好。np!如果它有帮助,不要忘记接受它,因为它可能对将来的其他人有用!快乐编码。。。