我制作了这个Rock Paper剪刀游戏,但我想知道如何在用户输入R,P,S以外的东西时显示无效错误。如果任何一次可以告诉我如何制作它以便它做到这一点将是有帮助的。我是一个相对较新的编码器。在此先感谢您的所有帮助
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args)
{
String userPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt;
String response;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Lets play Rock, Paper, Scissors!\n" +
"Choose your move.\n" + "Rock = R, Paper" +
"= P, and Scissors = S.");
System.out.println();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3)+1;
//Translate computer's randomly generated play to
//string using if //statements
if (computerInt == 1)
computerPlay = "R";
else if (computerInt == 2)
computerPlay = "P";
else if (computerInt == 3)
computerPlay = "S";
//Get player's play from input-- note that this is r
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
//Print computer's play
System.out.println("Your opponents play is: " + computerPlay);
//See who won.
if (userPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (userPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock breaks scissors. You win!!");
else if (computerPlay.equals("P"))
System.out.println("Paper covers rock. You lose!!");
else if (userPlay.equals("P"))
if (computerPlay.equals("S"))
System.out.println("Scissor cuts paper. You lose!!");
else if (computerPlay.equals("R"))
System.out.println("Paper covers rock. You win!!");
else if (userPlay.equals("S"))
if (computerPlay.equals("P"))
System.out.println("Scissor cuts paper. You win!!");
else if (computerPlay.equals("R"))
System.out.println("Rock breaks scissors. You lose!!");
else
System.out.println("Invalid user input.");
}
}
答案 0 :(得分:0)
我认为最好的方法是使用循环。你要求进入玩家的行动,如果不可接受,你再问一次。你继续它直到用户输入有效的字符串。在代码中它看起来像这样
ID name value1 value2
1 x a,b,c x
2 y d,r z
如果要显示错误然后停止程序,而不是创建循环,则可以使用类似条件的简单//Get player's play from input
boolean moveOk = false;
while (moveOk == false) {
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
// check that the input is ok
if ("R".equals(userPlay) ||
"P".equals(userPlay) ||
"S".equals(userPlay)) {
moveOk = true;
} else {
System.out.println("Bad input, try again!");
}
}
,当输入错误时打印错误消息并使用if
。
如果您将条件分成几部分,例如通过用户输入,您也可以使代码更容易理解。见下面的例子。
return
您也可以使用//See who won.
String result = "";
if (userPlay.equals(computerPlay)) {
result = "It's a tie!";
}
if (userPlay.equals("R")) {
if (computerPlay.equals("S")) {
result = "Rock breaks scissors. You win!!";
}
if (computerPlay.equals("P")) {
result = "Paper covers rock. You lose!!";
}
}
if (userPlay.equals("P")) {
if (computerPlay.equals("S")) {
result = "Scissor cuts paper. You lose!!";
}
if (computerPlay.equals("R")) {
result = "Paper covers rock. You win!!";
}
}
if (userPlay.equals("S")) {
if (computerPlay.equals("P")) {
result = "Scissor cuts paper. You win!!";
}
if (computerPlay.equals("R")) {
result = "Rock breaks scissors. You lose!!";
}
}
System.out.println(result);
声明。