我正在尝试做 roshambo(或 Rock-Paper-Scissors)并让计算机输出 Rock、Paper 或 Scissors 来代替从 0 到 2 的随机值。但是,我没有得到随机值。当程序运行时,它会经过一个while循环,并且每次经过循环时,值都保持不变。但是,如果我停止,然后重新运行应用程序,值会发生变化,并在循环期间保持相同的值。我是否正确编码此方法?
public class Player2 extends Player{
private Random rand;
public Player2(){
rand = new Random();
}
public Roshambo generateRoshambo() {
int min = 0;
int max = 2;
int randomNum = rand.nextInt(max - min + 1) + min;
if(randomNum == 0){
return Roshambo.ROCK;
} else if (randomNum == 1) {
return Roshambo.PAPER;
} else {
return Roshambo.SCISSORS;
}
}
}
这是循环:
Roshambo value = p2.generateRoshambo();
String cont = "Yes";
do{
System.out.print("\nRock, Paper, or Scissors: ");
String choice = sc.nextLine();
System.out.println("\n" + name + ": " + choice);
System.out.println("Computer: " + value);
if (value == ...)
{
System.out.println("\n" + name + " Wins!\n");
}
else if (value == ...)
{
System.out.println("\nYou Tied!\n");
}
else
{
System.out.println("\nThe Computer Wins!\n");
}
System.out.print("Play again? (Yes/No): ");
cont = sc.nextLine();
} while (cont.equalsIgnoreCase("Yes"));