0

我一直在尝试初始化字符串,但无济于事。我已经尝试了我遇到的所有解决方案,但我不确定是因为我的无能还是因为我需要一个新的解决方案我已经制定了程序的逻辑,所以我只需要帮助尝试初始化字符串值。如果有人可以提供帮助,我将不胜感激!

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 + ".");

}

}

4

4 回答 4

1
String computer; // not initialized

将其更改为

String computer = null;  or ""// initialized
if (player.compareTo(computer) == 0)

因为,变量计算机将根据条件分配。如果您提到的上述条件不满足。该值为无,因此仅显示错误。

于 2013-10-24T02:14:10.917 回答
1

编译器看到你有字符串计算机。他还可以看到这台计算机已针对答案 1 或 2 或 3 进行了初始化。但由于它无法读取逻辑,因此它不知道答案是否可以或不能更低或更高,因此它假设最坏的可能性 - 那台计算机不会被初始化。

只需将您的上线更改为

String computer = "";

应该没问题。

于 2013-10-24T02:15:30.473 回答
0

如果我完全理解您的问题,请记住您不能在 Java 中执行此操作:

String str = "here is some
    random text";

你能做的是

String str = "here is some " + 
    "random text";

换句话说 - 不能在行尾断开字符串,必须关闭它并使用+符号。

于 2013-10-24T02:14:14.217 回答
0

Strings您可以通过两种方式 初始化 的值:

String abc = "I love basketball";

或者

String abc = new String("I love basketball");  

您看到的是警告而不是错误。如果您坚持不将值放入 中String,请执行

String abc = null;    

或者

String abc = ""  

在后一种情况下,您的 String 被初始化为引号之间的字符。没有,基本上,但它不是null

为避免潜在NPE问题,请String按以下方式比较 s:

"rock".equalsIgnoreCase(playerChoice);
于 2013-10-24T02:14:23.907 回答