你想要几个骰子点? 6 试1:2 试2:4 试3:5 试4:6 在4次尝试中,你发现了6点
所以,这就是我试图得到的输出,但似乎找不到方法。
System.out.println("how many dice dots do u want?");
int dots = s.nextInt();
int dots2 = (int) (6 * Math.random()) + 1;这就是我所拥有的。我现在试了一个for循环,但它似乎不起作用。有人能帮我吗?
发布于 2018-01-04 11:47:58
你可以用这样的方式实现你想要的:
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);希望这能帮上忙..。快乐的编码!
发布于 2018-01-04 11:41:29
请下次把你的问题写好,这有助于我们帮助你:)
你需要一个dice循环来掷骰子。
int dots = s.nextInt();
boolean found = false;
Random rnd = new Random();
while(!found) {
int rolled= rnd.nextInt(6) + 1;
if(rolled == dots) // do your stuff
found = true;
}希望能帮上忙。
https://stackoverflow.com/questions/48094393
复制相似问题