gpt4 book ai didi

java - 为什么这个开关 block 不起作用?

转载 作者:行者123 更新时间:2023-12-01 23:13:35 25 4
gpt4 key购买 nike

我正在编写一些代码来计算层次结构的特定估值(以点为单位)。我在扫描仪中使用 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 语句 - 事实上,您用 { } 包围一个 block 不会使其中断:

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 - 为什么这个开关 block 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551106/

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