我正在编写一些代码来计算层次结构的某种估值(以点数表示)。我正在使用带有扫描器的switch语句(声明为静态变量),并且由于某种原因,在每个boardTypes中,“y”和“n”情况不会中断;他们只是进入下一个boardType案例。我在这做错了什么?
System.out.println("Are they on board? (y/n)");
isOnBoard = s.nextLine();
switch (isOnBoard){
case "y":
System.out.println("Chapter, regional, or international board? ");
boardType = s.nextLine();
switch (boardType){
case "chapter":
System.out.println("Are they chapter president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = chapterPres;
break;
case "n":
totalPoints = chapterBoardMember;
break;
}
case "regional":
System.out.println("Are they regional president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = regionalPres;
break;
case "n":
totalPoints = regionalExecutive;
break;
}
case "international":
System.out.println("Are they international president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = internationalPres;
break;
case "n":
totalPoints = internationalExecutive;
break;
}
case "n": break;
}
全部谢谢!
答案 0 :(得分:3)
您缺少break
个陈述 - 您使用{ }
附上一个块的事实不会让它崩溃:
case "chapter":
System.out.println("Are they chapter president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = chapterPres;
break;
case "n":
totalPoints = chapterBoardMember;
break;
}
break; // This was missing
case "regional":
System.out.println("Are they regional president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = regionalPres;
break;
case "n":
totalPoints = regionalExecutive;
break;
}
break; // This was missing
case "international":
System.out.println("Are they international president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = internationalPres;
break;
case "n":
totalPoints = internationalExecutive;
break;
}
break; // This was missing
case "n": break;
答案 1 :(得分:0)
switch
秒内您有switch
个。 break
语句突破了内部语句,但不是外部语句。外部的你也需要break
:
switch (boardType){
case "chapter":
System.out.println("Are they chapter president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = chapterPres;
break; // <== Only breaks out of the inner `switch(s.nextLine())`
case "n":
totalPoints = chapterBoardMember;
break; // <== Only breaks out of the inner `switch(s.nextLine())`
}
break; // <=== Need this (and so on) to break out of outer switch
case "regional":
// ...
答案 2 :(得分:0)
你从内部情况突然而不是从外部突破。你也必须休息一下外壳。
答案 3 :(得分:0)
You can change your code with break statement for outer cases too
------------------------------------------------------------------------------
System.out.println("Are they on board? (y/n)");
isOnBoard = s.nextLine();
switch (isOnBoard){
case "y":
System.out.println("Chapter, regional, or international board? ");
boardType = s.nextLine();
switch (boardType){
case "chapter":
System.out.println("Are they chapter president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = chapterPres;
break;
case "n":
totalPoints = chapterBoardMember;
break;
}
break;
case "regional":
System.out.println("Are they regional president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = regionalPres;
break;
case "n":
totalPoints = regionalExecutive;
break;
}
break;
case "international":
System.out.println("Are they international president? (y/n)");
switch(s.nextLine()){
case "y":
totalPoints = internationalPres;
break;
case "n":
totalPoints = internationalExecutive;
break;
}
break;
case "n": break;
}