Java 为什么不是';这个开关不工作吗?
我正在编写一些代码来计算层次结构的特定估值(以点为单位)。我将switch语句与scanner(声明为静态变量)一起使用,由于某种原因,在每个boardtype中,“y”和“n”的情况不会中断;他们只是进入下一个木板箱。我做错了什么Java 为什么不是';这个开关不工作吗?,java,switch-statement,Java,Switch Statement,我正在编写一些代码来计算层次结构的特定估值(以点为单位)。我将switch语句与scanner(声明为静态变量)一起使用,由于某种原因,在每个boardtype中,“y”和“n”的情况不会中断;他们只是进入下一个木板箱。我做错了什么 System.out.println("Are they on board? (y/n)"); isOnBoard = s.nextLine(); switch (isOnBoard){ case "y": System.out.println("Chapt
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;
}
谢谢大家 您在
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":
// ...
开关(板式){
案例“章节”:
System.out.println(“他们是分会主席吗?(y/n)”);
开关(s.nextLine()){
案例“y”:
总点数=分位数;
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":
// ...
开关(板式){
案例“章节”:
System.out.println(“他们是分会主席吗?(y/n)”);
开关(s.nextLine()){
案例“y”:
总点数=分位数;
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":
// ...
开关(板式){
案例“章节”:
System.out.println(“他们是分会主席吗?(y/n)”);
开关(s.nextLine()){
案例“y”:
总点数=分位数;
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":
// ...
开关(板式){
案例“章节”:
System.out.println(“他们是分会主席吗?(y/n)”);
开关(s.nextLine()){
案例“y”:
总点数=分位数;
打破;//你从内壳打破,而不是从外壳打破。你还必须从内壳打破,而不是从外壳打破。你必须从内壳打破,而不是从外壳打破。你必须从内壳打破,而不是从外壳打破。你还必须在外壳上打破。你呼吸k从内盒中取出,而不是从外盒中取出。您还必须在外盒上放置一个中断。您缺少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;
您缺少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;
您缺少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;
您缺少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;
这正是我搞砸的。非常感谢…我仍然是一个Java初学者:这正是我搞砸的。非常感谢…我仍然是一个Java初学者:这正是我搞砸的。非常感谢…我仍然是一个Java初学者:这正是我搞砸的。非常感谢…我仍然是我是一个Java初学者程序员:P