这是错误消息
----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 回答
在这一节这里:
computerInt
保证为1,2或3,因此必须初始化computerPlay
. 但这并非显而易见(它需要理解nextInt(int)
的目的,并且知道它已正确实现),因此编译器不知道这一点 . 据他所知,computerInt
可以是任何有效的int
,并且大多数这些值导致computerPlay
未正确初始化 .所以你应该确保通过代码的每个可想到的路径设置一个值 . 在您的情况下,这是一个非常简单的变化:
通过删除最终的
if
,该值将始终初始化 . 删除该检查是安全的,因为您知道如果它到达else
它将始终被初始化,即使编译器没有 .其他选项是在声明
computerPlay
(例如""
)时设置默认值,或者在结束时使用else
正确处理不是预期值的情况(如果稍后更改代码以使其可以是另一个值) , 例如) .将空String / null分配给它:
要么
您也可以稍微修改一下代码:
这样你的computerPlay变量将始终被初始化 .
以下代码后显示错误:
computerInt等于1或2或3,但编译器现在没有 . nextInt()方法可以从编译器的角度返回任何内容 .
这意味着computerPlay可能尚未初始化 . 你是这样做的:
但是如果他在任何情况下都不合适,那么对于computerPlay没有默认值,因此该值将是垃圾并使程序崩溃 . 让你的程序编译 . 您可以向计算机Play添加默认值,如下所示:
或者在if语句中添加另一个将成为默认值的else .
如果不能保证它们将在程序中稍后初始化,则需要在声明语句中初始化变量 . 例如,if / else-if语句不能确保初始化computerPlay,因为有可能不会执行if / else-if语句 . 要解决此问题,请更改此行代码,
对此
这是一个例外 . 当变量在它们被使用的子路径/方法之外声明时,它们不必是initiazlied
无论如何你're getting that error is because it is possible (in the compiler'查看'computerPlay'没有获得值的原因 . 唯一一次
computerPlay
被分配到这里:但是如果
computerInt
是4呢?还是0?还是-273?编译器没有将值限制为1,2和3,因此它假定任何其他数字都可能,这意味着computerPlay
可能无法初始化 .要解决此问题,请将最后一个
else if
更改为else
,或在其定义中将默认值更改为computerPlay
...即int computerPlay = 0;