[英]Why isn't this switch block working?
我正在編寫一些代碼來計算層次結構的某個評估(以點數表示)。 我在掃描器上使用了switch語句(聲明為靜態變量),由於某種原因,在每個boardType中,“ 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;
}
謝謝大家!
您缺少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;
您在switch
s內有switch
s。 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":
// ...
您從內殼而不是外層突圍。 您還必須休息一下外殼。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.