Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';这个开关不工作吗?_Java_Switch Statement - Fatal编程技术网

Java 为什么不是';这个开关不工作吗?

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

我正在编写一些代码来计算层次结构的特定估值(以点为单位)。我将switch语句与scanner(声明为静态变量)一起使用,由于某种原因,在每个boardtype中,“y”和“n”的情况不会中断;他们只是进入下一个木板箱。我做错了什么

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