我一直在尝试初始化字符串,但无济于事。我已经尝试了我遇到的所有解决方案,但我不确定是因为我的无能还是因为我需要一个新的解决方案我已经制定了程序的逻辑,所以我只需要帮助尝试初始化字符串值。如果有人可以提供帮助,我将不胜感激!
PS诅咒我想要挑战并使用srings。-_-
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
String player, computer;
int answer;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(3) + 1;
if (answer < 1 || answer > 3)
answer = generator.nextInt(3) + 1;
if (answer == 1)
computer = "rock";
if (answer == 2)
computer = "paper";
if (answer == 3)
computer = "scissors";
System.out.println ("Please choose rock, paper, or scissors.");
player = scan.nextLine();
if (!player.equalsIgnoreCase("rock") || !player.equalsIgnoreCase("paper")
|| !player.equalsIgnoreCase("scissors"))
{
System.out.println ("Please correctly enter one of the three
choices: rock, paper, and scissors.");
player = scan.nextLine();
}
if (player.compareTo(computer) == 0)
System.out.println ("It's a draw! You both chose " + player +
"!");
if ((computer.equalsIgnoreCase("rock") &&
player.equalsIgnoreCase("scissors")) &&
(computer.equalsIgnoreCase("scissors") && player.equalsIgnoreCase("paper"))
&& (computer.equalsIgnoreCase("paper") && player.equalsIgnoreCase("rock")))
System.out.println ("You lost! The computer chose " + computer + "
and you chose " + player + ".");
if ((player.equalsIgnoreCase("rock") &&
computer.equalsIgnoreCase("scissors")) &&
(player.equalsIgnoreCase("scissors") &&
computer.equalsIgnoreCase("paper")) &&
(player.equalsIgnoreCase("paper") &&
computer.equalsIgnoreCase("rock")))
System.out.println ("You won! CONGRATULATIONS! The computer chose
" + computer + " and you chose " + player + ".");
}
}