在 Java 中使用随机类的简单投币。do while 循环似乎不会产生随机结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24174078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Coin Toss using random class in Java. The do while loop doesn't seem to generate random results
提问by Mac
Sorry for the basic question but I'm new to Java.
对不起,基本问题,但我是 Java 新手。
I'm having issues generating the random number each time I run through the do-while loop in main. When I remove the do - while statement the if statement works fine and seems to generate a random result each time but when it is repeated in the loop it seems to just repeat the initial result.
每次运行 main 中的 do-while 循环时,我都会遇到生成随机数的问题。当我删除 do - while 语句时,if 语句工作正常,似乎每次都会生成一个随机结果,但是当它在循环中重复时,它似乎只是重复了初始结果。
Here is my code:
这是我的代码:
import java.util.Random;
public class CoinToss {
private enum Coin{Heads, Tails};
Random randomNum = new Random();
private int result = randomNum.nextInt(2);
private int heads = 0;
private int tails = 1;
Coin coinFlip;
public void flip(){
if(result == 0){
coinFlip = Coin.Heads;
System.out.println("You flipped Heads!");
}else{
coinFlip = Coin.Tails;
System.out.println("You flipped Tails!");
}
}
}
And my method main where I seem to be having issues:
我的方法主要在我似乎遇到问题的地方:
import java.util.Scanner;
public class TossGame {
public static void main(String[]args){
CoinToss test = new CoinToss();
int choice;
System.out.println("Welcome to the coin toss game!");
do{
System.out.print("Enter 1 to toss coin or enter 0 to quit: ");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
if (choice == 1){
test.flip();
}
else if (choice > 1){
System.out.println("Invalid entry - please enter 1 or 0: ");
choice = input.nextInt();
}
}while (choice != 0);
}
}
Thanks in advance for your suggestions!
预先感谢您的建议!
采纳答案by nook
You need to put result inside of flip()
method, so it is randomized each call.
您需要将结果放在flip()
方法中,因此每次调用都会随机化。
private int result;
private int heads = 0;
private int tails = 1;
Coin coinFlip;
public void flip(){
result = randomNum.nextInt(2)
if(result == 0){
coinFlip = Coin.Heads;
System.out.println("You flipped Heads!");
}else{
coinFlip = Coin.Tails;
System.out.println("You flipped Tails!");
}
}
}
回答by rgettman
You only "flipped" once, when you initialized result
:
初始化时,您只“翻转”一次result
:
private int result = randomNum.nextInt(2);
When you call flip
, get another result:
当您调用 时flip
,会得到另一个结果:
public void flip(){
result = randomNum.nextInt(2); // Add this line
if(result == 0){
回答by bstar55
You need to randomize a flip each time you call flip(), not when CoinToss is constructed:
每次调用 flip() 时都需要随机化翻转,而不是在构造 CoinToss 时:
public class CoinToss {
private enum Coin{Heads, Tails};
Random randomNum = new Random();
private int result;
private int heads = 0;
private int tails = 1;
Coin coinFlip;
public void flip(){
result = randomNum.nextInt(2)
if(result == 0){
coinFlip = Coin.Heads;
System.out.println("You flipped Heads!");
}else{
coinFlip = Coin.Tails;
System.out.println("You flipped Tails!");
}
}