我正在尝试制作一个最好的两个三分之一的摇滚剪刀程序,计算机随机滚动0-2,每个都分配到岩石,纸张或剪刀,然后它比较userInput并为计算机或播放器计算胜利,然后将其加起来。
但是,我无法弄清楚如何使用如果用户输入“剪刀”,程序会知道它也被分配给2(用于比较目的)。
public static void main(String[] args) {
Random r = new Random();
int gameCount = 0;
int computerWins = 0;
int playerWins = 0;
int rock = 0;
int paper = 1;
int scissors = 2;
int playerChoice;
int computerChoice = r.nextInt(3);
System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");
while (gameCount >= 0 && gameCount < 3)
{
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\"");
break;
}
playerChoice = userInput.nextInt()
//If player enters anything besides rock, paper, or scissors
if (playerChoice < 0 || playerChoice >= 3) {
System.out.println("That wasn't an option");
computerWins++;
gameCount++;
//The game goes on, and the winners are added up!
} else if (playerChoice == 0 && computerChoice == 1) {
computerWins++;
gameCount++;
System.out.println("Rock v Paper! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 0) {
playerWins++;
gameCount++;
System.out.println("Paper v Rock! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 2) {
computerWins++;
gameCount++;
System.out.println("Paper v Scissors! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 1) {
playerWins++;
gameCount++;
System.out.println("Scissors v Paper! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 0) {
computerWins++;
gameCount++;
System.out.println("Scissors v Rock! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 2) {
playerWins++;
gameCount++;
System.out.println("Rock v Scissors! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 0) {
gameCount++;
System.out.println("Rock v Rock! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 1) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 2) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
}
//Check if game count reaches max games then chooses a winner
if (gameCount == 3 && computerWins > playerWins) {
System.out.println("The Computer Wins!");
} else if (gameCount == 3 && computerWins < playerWins) {
System.out.println("The Player Wins!");
} else if (gameCount == 3 && computerWins == playerWins) {
System.out.println("The game is a tie!");
}
}
}
答案 0 :(得分:0)
所以不要playerChoice = userInput.nextInt();
尝试这个:
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
try {
playerChoice = Integer.parseInt(input);
} catch (NumberFormatException e) {
if (input.equalsIgnoreCase("rock")) {
playerChoice = rock;
} else if (input.equalsIgnoreCase("paper")) {
playerChoice = paper;
} else if (input.equalsIgnoreCase("scissors")) {
playerChoice = scissors;
} else {
// if input is invalid
playerChoice = -1;
}
}
由于您使用userInput.nextInt()
和playerChoice
是一个int并且只能保存整数,因此您需要解析用户的输入。在这种情况下,Integer.parseInt(input)
将尝试在用户输入中查找int。如果它不能返回异常;这就是为什么会有一个尝试捕获块的原因。如果它不是int,它将查找每个字符串并将关联的int值分配给playerChoice
或-1如果输入无效。然后你的其余代码应该能够在那之后适当地处理playerChoice。