Java 游戏石头剪刀布。无法弄清楚用户控制循环继续播放的主要方法

Java game Rock Paper Scissors. cant figure out main method user controlled loop to continue playing

正在开发一个 java 程序,该程序调用方法来玩剪刀石头布。差不多完成了,但我想在 main 方法中添加一个用户控制的循环,以继续播放直到用户输入 Sentinel 值。我需要有关循环的帮助。

我试过使用 do-while 循环,但是当我编译时我收到这条消息:

RockPaperScissorsSentinelLoop.java:23: error: cannot find symbol
      Sting winner; // Displays the winner of the game
      ^
  symbol:   class Sting
  location: class RockPaperScissorsSentinelLoop
1 error


 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
import java.util.Scanner;
import java.util.Random;
/** This is a program that simulates the children's game of rock, paper, scissors. The user enters 
his or her choice.
Then the computer makes its choice randomly. The rules are, rock smashes scissors, scissors cut 
paper, and paper wraps rock. if the user and computer make the same selection no one wins and the 
game continues.

@GetComputersChoice: This Class generates computers random choice and returns it to the main class.
@ShowMenu: This class just displays a menu of Strings to enter("Rock", "Paper" or "Scissors").
@GetUsersChoice: This class enters the users choice, and returns the value to the main class.
@ChooseWinner: This class determines the winner and returns the result to the main class.
*/

public class RockPaperScissorsSentinelLoop
{

public static void main(String[] args)
{
char letter; // holds sentinel value for posttest condition loop
Random random  = new Random(); // Returns Comuter random choice.
Scanner scanner = new Scanner(System.in); // reads inputs
String computersChoice; // Holds computers random choice
String usersChoice; // Holds users choice: Rock, Paper or Scissors
Sting winner; // Displays the winner of the game

do
{
System.out.println("Let's play Rock, Paper, Scissors\n");

ShowMenu();// Calls show menu method

computersChoice = GetComputersChoice(random); // calls GetComputersChoice and assings its 
argument value to computersChoice
usersChoice = GetUsersChoice(scanner); // calls GetUsersChoice and assings its argument value 
to UsersChoice 
System.out.println("\nYou chose " + usersChoice + "," + " The Computer chose " + 
computersChoice + "\n");
winner = ChooseWinner(computersChoice, usersChoice);/* Inherits the values of perameters 
computersChoice and usersChoice
calls the arguments of ChooseWinner class to determine the winner and 
assigns the value to string variable winner */
System.out.println(winner);
System.out.println("Or enter Q to quit");
String answer = scanner.nextLine();
letter = answer.charAt(0);

while(winner == "No winner")
{
System.out.println("You both chose the same weapon,\n" + "play again");
ShowMenu();// Calls show menu method

computersChoice = GetComputersChoice(random);   
usersChoice = GetUsersChoice(scanner); 
System.out.println("\nYou chose " + usersChoice + "," + " The Computer chose " + 
computersChoice + "\n");
winner = ChooseWinner(computersChoice, usersChoice);                                                                                                  
System.out.println(winner);
}
}
while (letter != 'Q' && letter != 'q'); //condition for while loop
}

public static String GetComputersChoice(Random random)
{

int wordNumber;
String computerWordChoice = "";

wordNumber = random.nextInt(3) +1; // 3 + 1 shifts number from 0-2 to 1-3

if (wordNumber == 1)
{
computerWordChoice = "Rock";
}
else if (wordNumber == 2)
{
computerWordChoice = "Paper";
}
else if (wordNumber == 3)
{
computerWordChoice = "Scissors";
}
System.out.print("The Computer has made its Choice:\n");

return computerWordChoice;
}

public static void ShowMenu()
{
System.out.println("Please Make a Choice\n 1. Rock\n 2. Paper\n 3. Scissors\n");
}

public static String GetUsersChoice(Scanner scanner)
{
String usersWordChoice;

System.out.print("User, type in your choice, and hit enter:");
usersWordChoice = scanner.nextLine();
return usersWordChoice;

}

public static String ChooseWinner(String computersChoice, String usersChoice)
{
String winner = "No winner";
String customMessage = "";
String finalMessage;

String rockCustomMessage = " Rock smashes Scissors";
String scissorsCustomMessage = " Scissors cut Paper";
String paperCustomMessage = " Paper wraps Rock";


if (computersChoice.equals("Rock") && usersChoice.equalsIgnoreCase("Scissors"))
{  
customMessage = rockCustomMessage;
winner = "Computer won";
}
else if (usersChoice.equalsIgnoreCase("Rock") && computersChoice.equals("Scissors"))
{
customMessage = rockCustomMessage;
winner = "You won";
}

if (computersChoice.equals("Scissors") && usersChoice.equalsIgnoreCase("Paper"))
{  
customMessage = scissorsCustomMessage;
winner = "Computer won";
}
else if (usersChoice.equalsIgnoreCase("Scissors") && computersChoice.equals("Paper"))
{
customMessage = scissorsCustomMessage;
winner = "You won";
}

if (computersChoice.equals("Paper") && usersChoice.equalsIgnoreCase("Rock"))
{  
customMessage = paperCustomMessage;
winner = "Computer won";
}
else if (usersChoice.equalsIgnoreCase("Paper") && computersChoice.equals("Rock"))
{
customMessage = paperCustomMessage;
winner = "You won";
}
finalMessage = winner + "." + " " + customMessage + "\n";
return finalMessage;
}  
} 

我检查了你的代码。我看到的唯一问题是,在没有赢家的情况下,您的 ChooseWinner 方法 returns "No winner.\n" 并且您将其与 "No winner" 进行比较,因此它们永远不会匹配。 另外,最好改变比较这两者的方式,使用类似这样的方式

while(winner.equals("No winner")){
   }