Java 案例Q可以';不能解析为变量
使用Java 案例Q可以';不能解析为变量,java,Java,使用case制作程序,案例为1、2和Q 案例1和2工作正常,但用于退出程序的案例Q无法解析为变量(Q无法解析为变量) 要遵循的代码段: System.out.print("-- MENU -- \n"); System.out.print("1: Display Journeys \n"); System.out.print("2: Identify Suitable Journeys \n"); System.out.print(
case
制作程序,案例为1
、2
和Q
案例1
和2
工作正常,但用于退出程序的案例Q
无法解析为变量(Q无法解析为变量
)
要遵循的代码段:
System.out.print("-- MENU -- \n");
System.out.print("1: Display Journeys \n");
System.out.print("2: Identify Suitable Journeys \n");
System.out.print("Q: Quit \n");
System.out.print("Pick an option: ");
int option = console.nextInt();
switch(option) {
case 1:
while(INPUT.hasNextLine()) {
System.out.println(INPUT.nextLine());
}
break;
case 2:
System.out.print("Specify desired location: ");
String destination = console.next();
System.out.print("Specify Max Time (HH:MM): ");
String choice = console.next();
// save the index of the colon
int colon = choice.indexOf(':');
// strip the hours preceding the colon then convert to int
int givenHours = Integer.parseInt(choice.substring(0, colon));
// strip the mins following the colon then convert to int
int givenMins = Integer.parseInt(choice.substring(colon + 1, choice.length()));
// calculate the time's total mins
int maxMins = (givenHours * 60) + givenMins;
System.out.print("Specify maximum changes: ");
int maxChange = console.nextInt();
// gui spacing
System.out.println();
// skips the first line which is York
INPUT.nextLine();
int mins = INPUT.nextInt();
int change = INPUT.nextInt();
if ((mins > maxMins) || ((change > maxChange)) && (destination.equals("York") || destination.equals("Alnwick"))) {
System.out.format("Time: %02d:%02d, Changes: %d = Unsuitable \n", (mins / 60), (mins % 60), change);
}
else {
System.out.format("Time: %02d:%02d, Changes: %d = Suitable \n", (mins / 60), (mins % 60), change);
}
//Do stuff
break;
case Q:
System.exit(0);
如果您使用的是Java7或更高版本,它增加了对在switch case语句中使用字符串的支持。所以,你可以做
案例'Q':
如果由于某种原因您不在Java 7上,请将用户输入读取为字符串而不是int,并在代码内部将用户输入的“Q”转换为整数。问题是您在int上使用开关表达式,因此1和2是有效值,但不带引号的Q是一个变量名,在您的程序中不存在 你的意思可能是“Q”或“Q”,这两个词都不起作用——因为它不是int 如果可选值为1、2和Q,则应使用字符串,并在您的情况下使用“1”、“2”和“Q” 这也需要改变:
int option = console.nextInt();
致:
早些时候,你有一句话:
int option = console.nextInt();
这要求从控制台读取int。由于“Q”无法解析为int,因此键入时将抛出错误
如果您想保留nextInt()
,我建议使用选项1、2和9;看到9时退出
---编辑:添加一些代码---
此代码经过测试并正常工作
public class SwitchCase {
public static void main(String[] args) {
System.out.print("-- MENU -- \n");
System.out.print("1: Display Journeys \n");
System.out.print("2: Identify Suitable Journeys \n");
System.out.print("Q: Quit \n");
System.out.print("Pick an option: ");
try {
char c = (char) System.in.read();
switch (c) {
case '1':
System.out.println("You chose 1.");
break;
case '2':
System.out.println("You chose 2.");
break;
case 'Q':
case 'q':
System.out.println("Quitting.");
System.exit(0);
break;
}
} catch (IOException e) {
System.out.println("You chose something that I couldn't read.");
System.exit(1);
}
}
}
也许你想读一个
字符
,并将其与'1'
、'2'
和'Q'
进行比较?在任何情况下,您都不能只使用Q
而不在某个地方定义它。我肯定这就是我一直在寻找的,但现在我遇到了一个不同的错误:类型扫描程序的方法nextString()未定义
public class SwitchCase {
public static void main(String[] args) {
System.out.print("-- MENU -- \n");
System.out.print("1: Display Journeys \n");
System.out.print("2: Identify Suitable Journeys \n");
System.out.print("Q: Quit \n");
System.out.print("Pick an option: ");
try {
char c = (char) System.in.read();
switch (c) {
case '1':
System.out.println("You chose 1.");
break;
case '2':
System.out.println("You chose 2.");
break;
case 'Q':
case 'q':
System.out.println("Quitting.");
System.exit(0);
break;
}
} catch (IOException e) {
System.out.println("You chose something that I couldn't read.");
System.exit(1);
}
}
}