Java игра Каменные ножницы. не могу понять основной метод контролируемый пользователем цикл, чтобы продолжить игру - PullRequest
1 голос
/ 05 октября 2019

Работа над 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;
}  
} 

1 Ответ

0 голосов
/ 06 октября 2019

Я просмотрел ваш код. Единственная проблема, с которой я столкнулся, заключалась в том, что в случае отсутствия победителя ваш метод ChooseWinner возвращает «Нет победителя. \ n», а вы сравниваете это с «Без победителя», поэтому они никогда не совпадают. Кроме того, лучше изменить способ сравнения этих двух параметров, например,

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