我一直在嘗試獲取字符串初始化,但無濟於事。我已經嘗試過所有我遇到的解決方案,但我不確定是因爲我的能力不足還是因爲我需要一個新的解決方案,我已經掌握了該程序的邏輯,所以我只需要幫助,試圖初始化字符串值。如果有人能夠幫助,我會非常感激!字符串變量可能尚未初始化(錯誤dieces到第34行)
P.S詛咒我想要挑戰和使用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 + ".");
}
}
因此,當你聲明它們時,將你的字符串變量設置爲「」。 – OldProgrammer