我的任务是编写一个程序,使用户可以在计算机上玩摇滚,剪纸,剪刀游戏。
说明:
main方法应具有两个嵌套循环,其中外循环将允许用户根据需要频繁地玩游戏,而内循环将在有平局的情况下玩游戏。在userChoice()方法的while循环中调用方法isValidChoice(),以验证用户输入的选择必须是“石头”,“纸”还是“剪刀”。如果输入了无效的字符串,则isValidChoice()将返回false,程序应请求新输入,直到给出有效输入为止。
情况:
当用户输入有效输入时,程序运行正常。但是,一旦它不是有效的输入,就会有一个小问题。
结果:
Enter rock, paper, or scissors: rocky
Invalid input, enter rock, paper, or scissors: roc
Invalid input, enter rock, paper, or scissors: rock
The computer's choice was paper
The user's choice was rocky
Play again? (y/n)
如您所见,程序会识别无效的输入。用户最后输入一个有效的
第三次输入。但是,它显示用户的首选“ rocky”,这是无效的。
因此,该程序无法显示谁获胜。
题
我需要你的帮助。
我希望我的程序像这样运行:
当用户输入多个无效输入但一次
输入有效输入后,我的程序仍应能够显示用户的有效输入并显示获胜者。
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissorsGame
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
String computer, user;
char keepPlaying;
do
{
computer = computerChoice();
user = userChoice();
System.out.println("The computer's choice was " + computer);
System.out.println("The user's choice was " + user);
determineWinner(computer, user);
while (computer.equals(user))
{
computer = computerChoice();
user = userChoice();
System.out.println("The computer's choice was " + computer);
System.out.println("The user's choice was " + user);
determineWinner(computer, user);
}
System.out.println("\n" + "Play again? (y/n)");
keepPlaying = keyboard.nextLine().toLowerCase().charAt(0);
while ( keepPlaying != 'y' && keepPlaying != 'n' )
{
System.out.println("Invalid input, please enter (y/n)");
keepPlaying = keyboard.nextLine().toLowerCase().charAt(0);
}
} while (keepPlaying == 'y');
}
public static String computerChoice()
{
String[] choice = {"rock","paper","scissors"};
Random rand = new Random();
int computerChoice = rand.nextInt(3);
return choice[computerChoice];
}
public static String userChoice()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter rock, paper, or scissors: ");
String choice = keyboard.nextLine();
isValidChoice(choice);
return choice;
}
public static boolean isValidChoice (String choice)
{
Scanner keyboard = new Scanner(System.in);
while (!(choice.equalsIgnoreCase("rock")) && !(choice.equalsIgnoreCase("paper")) && !(choice.equalsIgnoreCase("scissors")))
{
System.out.print("Invalid input, enter rock, paper, or scissors: ");
choice = keyboard.nextLine();
}
return true;
}
public static void determineWinner(String computer, String user)
{
if (computer.equalsIgnoreCase("rock") && user.equalsIgnoreCase("paper"))
System.out.println("\n" + "Paper wraps rock.\nThe user wins!");
else if (computer.equalsIgnoreCase("rock") && user.equalsIgnoreCase("scissors"))
System.out.println("\n" + "Rock smashes scissors.\nThe computer wins!");
else if (computer.equalsIgnoreCase("paper") && user.equalsIgnoreCase("rock"))
System.out.println("\n" + "Paper wraps rock.\nThe computer wins!");
else if (computer.equalsIgnoreCase("paper") && user.equalsIgnoreCase("scissors"))
System.out.println("\n" + "Scissors cuts paper.\nThe user wins!");
else if (computer.equalsIgnoreCase("scissors") && user.equalsIgnoreCase("rock"))
System.out.println("\n" + "Rock smashes scissors.\nThe user wins!");
else if (computer.equalsIgnoreCase("scissors") && user.equalsIgnoreCase("paper"))
System.out.println("\n" + "Scissors cuts paper.\nThe computer wins!");
else if (computer.equalsIgnoreCase(user))
System.out.println("\n" + "The game is tied!\nGet ready to play again...");
}
}
最佳答案
此问题源于Java是按值传递而不是按引用传递的事实。
换句话说,传递的不是参数的引用,而是传递给方法的副本。您的方法更改了副本,但是一旦方法结束,副本将超出范围并被垃圾回收。
将choice
传递到isvalidChoice
时,不会传递对选择本身的引用。复制字符串,然后将其传递。更新变量选择时,它不会更改原始字符串,而是会更改系统创建的副本。 This answer explains how it works pretty well.
那你该怎么办?
如果不是有效输入,请让isValidChoice
返回false而不是循环进入isValidChoice
。
您的isValidChoice
应该最终看起来像这样:
public static boolean isValidChoice(String choice) {
return (choice.equalsIgnoreCase("rock")) || (choice.equalsIgnoreCase("paper")) || (choice.equalsIgnoreCase("scissors"));
}
然后在
userChoice
中执行类似的操作Scanner keyboard = new Scanner(System.in);
while(!isValidChoice(choice)) {
System.out.print("Invalid input, enter rock, paper, or scissors: ");
choice = keyboard.nextLine();
}