每当我运行它时,似乎继续玩的循环都有效,但是每当 computerChoose 执行 randomGenerator 时,游戏结果都不会正确输出。请帮忙。我是java新手,我们只打算使用3种方法——指令、playGame和computerChoose。我们还假设使用用户控制的循环来继续工作。我似乎无法做到这一点,我仍然需要添加一个循环来计算游戏已玩的次数、获胜的次数以及计算机获胜的次数。
import java.util.*;
public class PRS {
public static Scanner kbd = new Scanner(System.in);
public static void instructions() {
System.out.println("\nThis is the popular game of paper, rock, scissors. Enter your"
+ "\nchoice by typing the word \"paper\", the word \"rock\" or the word"
+ "\n\"scissors\". The computer will also make a choice from the three"
+ "\noptions. After you have entered your choice, the winner of the"
+ "\ngame will be determined according to the following rules:"
+ "\n\nPaper wraps rock (paper wins)"
+ "\nRock breaks scissors (rock wins)"
+ "\nScissors cuts paper (scissors wins)"
+ "\n\nIf both you and the computer enter the same choice, then the game "
+ "\nis tied.\n");
}
public static int playGame(){
int outcome = 0;
System.out.print("Enter your choice: ");
kbd = new Scanner(System.in);
String player = kbd.nextLine().toLowerCase();
String computerChoice = computerChoose();
System.out.println("\nYou entered: " + player);
System.out.println("Computer Chose: " + computerChoose());
if(player.equals(computerChoose())){
outcome = 3;
}
else if (player.equals("paper") && computerChoice.equals("rock")){
outcome = 1;
}
else if (computerChoice.equals("paper") && player.equals("rock")){
outcome = 2;
}
else if (player.equals("rock") && computerChoice.equals("scissors")){
outcome = 1;
}
else if (computerChoice.equals("rock") && player.equals("scissors")){
outcome = 2;
}
else if (player.equals("scissors") && computerChoice.equals("paper") ){
outcome = 1;
}
else if (computerChoice.equals("scissors") && player.equals("paper")){
outcome = 2;
}
else if (player.equals("rock") && computerChoice.equals("paper") ){
outcome = 2;
}
else if (computerChoice.equals("rock") && player.equals("paper")){
outcome = 1;
}
return outcome;
}
public static String computerChoose(){
/*return "scissors";*/
Random generator = new Random();
String [] answer = new String [3];
answer [0]= "paper";
answer [1] = "rock";
answer [2] = "scissors";
return answer[generator.nextInt(3)];
}
public static void main(String[] args) {
kbd = new Scanner(System.in);
System.out.println("THE GAME OF PAPER, ROCK, SCISSORS:");
System.out.print("\nDo you need instructions (Y or N)? ");
String userPlay = kbd.nextLine();
if (userPlay.equalsIgnoreCase("y")){
instructions();
}
String answer;
do{
int result = playGame();
System.out.println(result);
switch (result){
case 1:
System.out.println("YOU WIN!");
break;
case 2:
System.out.println("Comp WINs!");
break;
case 3:
System.out.println("IT'S A TIE!");
break;
default:
}
System.out.print("\nPlay again ( Y or N)? ");
answer = kbd.nextLine();
}while(answer.equalsIgnoreCase("y"));
}
}
最佳答案
您需要做的第一件事就是仅调用一次 computerChoose()
。每次调用此方法时,它都会生成一个新的随机数,从而产生不同的答案。您应该只在 playGame()
中调用它一次并将其分配给局部变量。
例如
String computerChoice = computerChoose();
然后用此变量名称替换对 computerChoose()
的所有其他调用。这样,您将显示一个值并仅比较逻辑中的一个值。
至于跟踪其他信息,例如游戏数量和获胜/失败的数量,请考虑声明更多的类变量(或主方法中的局部变量),然后您可以对其进行赋值、递增和读取。您可以在 main 方法的 do-while 循环中完成所有这些操作。不需要任何额外的循环。
关于java - 我无法让我的石头、剪刀、布程序读取正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26580476/