继续为我的RockPaperScissor程序收到此错误消息。我该如何解决这个错误?

时间:2015-12-15 16:24:52

标签: java jgrasp

这是错误消息

 ----jGRASP exec: javac -g RockPaperScissorRandom.java

RockPaperScissorRandom.java:40: error: variable computerPlay might not have been initialized
      **System.out.println("Computer play is: " + computerPlay);**
                                                ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

这是程序

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissorRandom{
   public static void main(String[]args){

      String personPlay; //User's play -- "0", "1", or "2"
      String computerPlay; //Computer's play -- "0", "1", or "2"
      int computerInt; //Randomly generated number used to determine computer's  play

      String response;

      Scanner scan= new Scanner(System.in);
      Random generator  = new Random();

      System.out.println("Hey, let's play Rock, Paper, Scissors!\n" + "Please enter a move.\n" + "Rock = 0, Paper = 1, and Scissors = 2.");

      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 = "0";
      else if (computerInt == 2)
         computerPlay = "1";
      else if (computerInt == 3)
         computerPlay = "2";

      //Get player's play from input-- not that this is stored as a string
      System.out.println ("Enter your play: ");
      personPlay = scan.next();

      //Make player's play uppercase ofr ease of comparison
      personPlay = personPlay.toUpperCase();

      //Print computer's play
      System.out.println("Computer play is: " + computerPlay);

      //See who won. use nested ifs

      if (personPlay.equals(computerPlay))
         System.out.println("It's a tie!");
      else if (personPlay.equals("0"))
         if (computerPlay.equals("2"))
         System.out.println("Rock crushes scissors. You WIN!!");
      else if (computerPlay.equals("1"))
         System.out.println("Paper eats rock. You LOSE!!");
      else if (personPlay.equals("1"))
         if (computerPlay.equals("2"))
         System.out.println("Scissors cuts paper. You LOSE!!");
      else if (computerPlay.equals("0"))
         System.out.println("Paper eats rock. You WIN!!");
      else if (personPlay.equals("2"))
         if (computerPlay.equals("1"))
         System.out.println("Scissors cuts paper. You WIN!!");
      else if (computerPlay.equals("0"))
         System.out.println("Rock breaks scissors. You LOSE!!");
      else
         System.out.println("Invalid user input.Computer wins by default. You LOSE!!");
   }
}

5 个答案:

答案 0 :(得分:4)

在本节中:

computerInt = generator.nextInt(3)+1;

if (computerInt == 1)
    computerPlay = "0";
else if (computerInt == 2)
    computerPlay = "1";
else if (computerInt == 3)
    computerPlay = "2";

computerInt保证为1,2或3,因此必须初始化computerPlay。但这并非显而易见(它需要理解nextInt(int)的目的,并且知道它已正确实现),因此编译器不知道这个。据他所知,computerInt可以是任何有效的int,其中大多数值导致computerPlay无法正确初始化。

因此,您应该确保通过代码的每个可想到的路径设置一个值。在您的案例中,这是一个非常简单的变化:

if (computerInt == 1)
    computerPlay = "0";
else if (computerInt == 2)
    computerPlay = "1";
else
    computerPlay = "2";

删除最终if后,该值将始终初始化。删除该检查是安全的,因为您知道如果它到达else它将始终被初始化,即使编译器没有。

其他选项是在声明computerPlay(例如"")时设置默认值,或者在结尾处设置else时正确处理不符合预期的情况值(例如,如果您稍后更改代码,以便它可以是另一个值)。

答案 1 :(得分:1)

为其分配空String / null:

String computerPlay = null;

String computerPlay = "";

您也可以稍微修改一下代码:

if (computerInt == 1)
    computerPlay = "0";
else if (computerInt == 2)
    computerPlay = "1";
else /* removed if (computerInt == 3)*/
    computerPlay = "2";

这样你的computerPlay变量将始终被初始化。

以下代码后显示错误:

computerInt = generator.nextInt(3)+1;

computerInt等于1或2或3,但编译器现在没有。 nextInt()方法可以从编译器的角度返回任何内容。

答案 2 :(得分:1)

这意味着computerPlay可能尚未初始化。 你这样做:

if (computerInt == 1)
     computerPlay = "0";
  else if (computerInt == 2)
     computerPlay = "1";
  else if (computerInt == 3)
     computerPlay = "2";

但是如果他在任何情况下都不适合,则没有默认值,因此该值将是垃圾并使您的程序崩溃。让你的程序编译。 您可以向计算机Play添加默认值,如下所示:

String computerPlay = "";

或在if语句中添加其他将成为默认值的其他内容。

if (computerInt == 1)
     computerPlay = "0";
  else if (computerInt == 2)
     computerPlay = "1";
  else if (computerInt == 3)
     computerPlay = "2";
  else
     computerPlay = "";

答案 3 :(得分:0)

如果无法保证稍后在程序中初始化变量,则需要在声明语句中初始化变量。 例如,if / else-if语句不能确保初始化computerPlay,因为有可能不会执行if / else-if语句。要解决此问题,请更改此行代码

String computerPlay;

到这个

String computerPlay = "";

这是一个例外。当变量在它们被使用的子路径/方法之外声明时,它们不必是initiazlied

答案 4 :(得分:0)

您收到该错误的原因是因为有可能(在编译器的视图中,无论如何),' computerPlay'没有得到价值。唯一一次分配computerPlay的地方是:

if (computerInt == 1)
   computerPlay = "0";
else if (computerInt == 2)
   computerPlay = "1";
else if (computerInt == 3)
   computerPlay = "2";

但如果computerInt是4,该怎么办?还是0?还是-273?编译器并不知道您已将值限制为1,2和3,因此它假定任何其他数字都可以,这意味着computerPlay可能无法初始化。

要解决此问题,请将上一个else if更改为else,或在其定义中将默认值指定为computerPlay ...即int computerPlay = 0;