【发布时间】:2018-10-23 04:52:17
【问题描述】:
是的,我知道这个问题有很多重复项。
以下代码在 BlueJ 中编译没有问题,但是,当在 Eclipse 中运行时,我得到以下行的错误:
while (selection != 'Q' && selection != 'q');
错误是:selection 无法解析为变量。
为什么我在 Eclipse 而不是 BlueJ 中遇到此错误?
public class menuMain {
public menuMain(String args) {
System.out.println("Welcome to the Project");
Scanner in = new Scanner(System.in);
do {
showMenu();
String menu = in.nextLine(); // read a line of input
char selection;
if (menu.length() > 0) {
selection = menu.toLowerCase().charAt(0); // extract the first char of the line read
}
else {
System.out.println("invalid input:\t"+selection);
System.out.println("Press enter to continue...");
Scanner itScan = new Scanner(System.in);
String nextIt = itScan.nextLine();
}
switch (selection)
{
case 'a':
changeTime time = new changeTime ();
break;
case 'b':
watchTime timeStop = new watchTime();
break;
case 'q':
System.out.println("\nEnding Now\n");
System.exit(0);
break;
default:
System.out.println("Instruction is invalid");
}
}
while (selection != 'Q' && selection != 'q');
System.exit(0);
}
}
【问题讨论】:
-
selection不在while部分能理解的范围内,这就是为什么,在do while循环外声明就行了 -
如果 BlueJ 没有将此称为错误,则 BlueJ 编译器不正确。
-
不知道@StephenC。在 Eclipse 中打开 BlueJ 文件还有一大堆其他问题。我缺乏经验可能是主要原因。