gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:42 26 4
gpt4 key购买 nike

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

我尝试使用 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 方法返回“无获胜者。\n”,并且您将其与“无获胜者”进行比较,因此它们永远不会匹配。另外,最好改变比较这两者的方式,使用这样的东西

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

关于Java游戏剪刀石头布。无法找出用户控制循环的主要方法来继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com