import java.util.Random;
public class dice
{
private int times;
private int roll;
private int side;
Random roller = new Random();
public void setTimes(int sides)
{
times = sides;
}
public void setSides(int die)
{
side = die;
}
public int getRoll()
{
int total; //here it is
int c = 0;
while (c <= times)
{
c = c + 1;
int rol = 0;
roll = roller.nextInt(side) + 1;
rol = rol + roll;
total = rol; //here it is initialized
}
return total; //here it says variable not initialized
}
}
最佳答案
while 循环的内部不能保证执行 - 例如,如果 times
由于编程错误而小于零。编译器知道这一点,因此在确定 total
是否已初始化时它不会依赖 while 循环。
关于java - Java 中的变量可能未初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4684905/