gpt4 book ai didi

java - 错误: orphaned default using case statement in Java

转载 作者:行者123 更新时间:2023-11-30 03:54:26 40 4
gpt4 key购买 nike

所以这是我的代码,我试图让用户在不输入满足先前情况的内容时必须重新输入。任何和所有的帮助表示赞赏。

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
ArrayList<Room> rooms = new ArrayList<Room>();
boolean proj, flag;
String rN, line, os = null;
int cap = 0;

int response = 0;
do {
System.out.println("Press: 1 to add a general purpose Room \n"
+ "Press: 2 to add a Computer Room \n"
+ "Press: 3 to display information about stored rooms \n"
+ "Press: 4 to display all rooms with projectors \n"
+ "Press: 5 to enter a number of attendees and display possible rooms. \n"
+ "Press: -1 to Exit");
response = kb.nextInt();
switch (response) {
case 1:
there is code here-
case 3:
there is code here-
case 4:
there is code here-
case 5:
there is code here-
case 6:
there is code here-

default:
System.out.println("Not understood. Re-enter");
break;

} while (response != -1);
}

我给出的错误是孤立的默认错误 - 我已从其他情况下删除了代码,因为它不允许我提出其他问题。

最佳答案

您的 while 语句似乎位于错误的位置,这可能会导致错误。

 do {
System.out.println("Press: 1 to add a general purpose Room \n"
+ "Press: 2 to add a Computer Room \n"
+ "Press: 3 to display information about stored rooms \n"
+ "Press: 4 to display all rooms with projectors \n"
+ "Press: 5 to enter a number of attendees and display possible rooms. \n"
+ "Press: -1 to Exit");

response = kb.nextInt();
switch (response)
{
case 1:
there is code here-
case 3:
there is code here-
case 4:
there is code here-
case 5:
there is code here-
case 6:
there is code here-
case -1:
System.out.println("Exiting program");
break;
default:
System.out.println("Not understood. Re-enter");
break;
}

}while (response != -1);

确保它位于 Do-While 循环的末尾,而不是像以前那样位于内部。

关于java - 错误: orphaned default using case statement in Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613028/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com