错误:Java中使用case语句的孤立默认值
这是我的代码,我试图让用户重新输入,如果他们没有输入满足前面案例的内容。感谢您的帮助错误:Java中使用case语句的孤立默认值,java,Java,这是我的代码,我试图让用户重新输入,如果他们没有输入满足前面案例的内容。感谢您的帮助 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; in
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);
}
publicstaticvoidmain(字符串[]args){
扫描仪kb=新扫描仪(System.in);
ArrayList房间=新建ArrayList();
布尔项目,标志;
字符串rN,line,os=null;
int cap=0;
int响应=0;
做{
System.out.println(“按:1添加通用房间\n”
+“按:2添加计算机室\n”
+“按:3显示有关存储室的信息\n”
+“按:4显示所有带投影仪的房间\n”
+“按:5输入与会者人数并显示可能的会议室。\n”
+“按:-1退出”);
response=kb.nextInt();
开关(响应){
案例1:
这里有代码-
案例3:
这里有代码-
案例4:
这里有代码-
案例5:
这里有代码-
案例6:
这里有代码-
违约:
System.out.println(“未理解。重新输入”);
打破
}while(响应!=-1);
}
我给出的错误是孤立的默认错误-我已经从其他情况下删除了代码,因为它不允许我提出其他问题。这对我很有用:
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
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-
break;
case 3:
// there is code here-
break;
case 4:
// there is code here-
break;
case 5:
// there is code here-
break;
case 6:
// there is code here-
break;
case -1:
System.out.println("Bye!");
break;
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循环的末尾,而不是像以前那样位于内部。我是在从以下内容中提取剩余内容时得到此信息的: 将交换机转换为if-else块。 给出“孤立默认值”错误的最小示例:
你是否在每一个“案例”的结尾都有一个“中断”?如果你能把它变成一个简短但完整的程序来演示问题(并且更合理地缩进你的代码),那会很有帮助。在“while”之前有一个}缺失。当用户输入-1时,这仍然会显示“未理解…”如果你已经更新了它,我更关心循环工作的语法。谢谢你让我知道。
public static void whatever(){
if( true ){
default: return;
};;
};;