how many dice dots do u want?
6
try 1 : 2
try 2 : 4
try 3 : 5
try 4 : 6
In 4 try's u found 6 dots
这就是我想要获得的输出,但似乎找不到如何实现。
System.out.println("how many dice dots do u want?");
int dots = s.nextInt();
int dots2 = (int) (6 * Math.random()) + 1;
这就是我所拥有的。我现在尝试了 for 循环,但似乎不起作用。有人可以帮助我吗?
最佳答案
您可以通过以下方式实现您想要的:
Scanner scan = new Scanner (System.in);
int randomValue = 0; //this is the random value that'd be autogenerated in each loop
int counter = 1; //this represents the number of trials
System.out.println("how many dice dots do u want?");
int dotsWanted = scan.nextInt();
while(randomValue != dotsWanted){
randomValue = (int) (6 * Math.random()) + 1;
counter++;
}
System.out.printf("In %d trials, %d dots were found\n", counter, dotsWanted);
我希望这会有所帮助......快乐编码!
关于java - 骰子模拟寻找正确的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094393/