private void northroom()
{
char choice2;
boolean boolean_1 = false;
String answer2;
System.out.println("You press a panel next to the door");
System.out.println("The panel lights up with a pinging sound");
System.out.println("The door slowly scrapes open");
System.out.println("You are now in North Room");
System.out.println("There is a object in the coner of the room shoraded by darkness");
do
{
System.out.println("Would you like to pick up object Yes(Y) or No(N)");
while(boolean_1 == false)
{
answer2 = keyboard.next();
choice2 = answer2.charAt(0);
if(choice2 == 'Y')
{
System.out.println("You go pick up the object");
}
if(choice2 == 'N')
{
System.out.println("Stare at object because you are useless");
System.out.println("Try again");
}
}
}
while (**choice2** != 'Y' && **choice2** != 'N');
while( choice2 !='Y'&& choice2 !='N');
这里两个choice2都被初始化,我如何纠正它,以便正确循环
答案 0 :(得分:1)
编译器代码路径分析器远远不足以认识到choice2
循环总是至少执行一次。
就编译器而言,可能会跳过循环体,在这种情况下char choice2 = ' ';
变量未分配,即明确分配。
您可以通过将其初始化为虚拟值来解决此问题,例如
while(boolean_1 == false)
当然,如果编译器进行了扩展分析,它会发现boolean_1
循环永远运行,因为while (choice2 != 'Y' && choice2 != 'N');
永远不会更新,并且会产生编译错误,说明{{1} 无法访问。
答案 1 :(得分:0)
我不确定嵌套循环的设计行为是什么。但是,你的内部循环是一个无限循环。它永远不会结束,所以外循环永远不能检查最后一行代码中显示的条件