错误:在Java中使用case语句的孤立默认值

时间:2014-05-12 15:26:00

标签: java

所以这里是我的代码,如果用户不输入满足之前案例的内容,我试图让用户重新输入。任何和所有帮助表示赞赏。

public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    ArrayList<Room> rooms = new ArrayList<Room>();
    boolean proj, flag;
    String rN, line, os = null;
    int cap = 0;

    int response = 0;
    do {
        System.out.println("Press: 1 to add a general purpose Room \n"
                + "Press: 2 to add a Computer Room \n"
                + "Press: 3 to display information about stored rooms \n"
                + "Press: 4 to display all rooms with projectors \n"
                + "Press: 5 to enter a number of attendees and display possible rooms. \n"
                + "Press: -1 to Exit");
        response = kb.nextInt();
        switch (response) {
            case 1:
                there is code here-
            case 3:
                there is code here-
            case 4:
                there is code here-
            case 5:
                there is code here-
            case 6:
                there is code here-

            default:
                System.out.println("Not understood. Re-enter");
                break;

                } while (response != -1);
        }

我给出的错误是孤立的默认值 - 我已将其他案例中的代码删除了,因为它不会让我这么做。

3 个答案:

答案 0 :(得分:1)

这对我有用:

import java.util.Scanner;

public class Test
{

    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        int response = 0;
        do
        {
            System.out.println("Press: 1 to add a general purpose Room \n" + "Press: 2 to add a Computer Room \n"
                               + "Press: 3 to display information about stored rooms \n"
                               + "Press: 4 to display all rooms with projectors \n"
                               + "Press: 5 to enter a number of attendees and display possible rooms. \n"
                               + "Press: -1 to Exit");
            response = kb.nextInt();
            switch (response)
            {
                case 1:
                    //                        there is code here-
                    break;
                case 3:
                    //                        there is code here-
                    break;
                case 4:
                    //                        there is code here-
                    break;
                case 5:
                    //                        there is code here-
                    break;
                case 6:
                    //                        there is code here-
                    break;

                case -1:
                    System.out.println("Bye!");
                    break;

                default:
                    System.out.println("Not understood. Re-enter");
                    break;

            }
        }
        while (response != -1);
    }
}

答案 1 :(得分:1)

好像你的while语句可能会导致错误。

 do {
    System.out.println("Press: 1 to add a general purpose Room \n"
            + "Press: 2 to add a Computer Room \n"
            + "Press: 3 to display information about stored rooms \n"
            + "Press: 4 to display all rooms with projectors \n"
            + "Press: 5 to enter a number of attendees and display possible rooms. \n"
            + "Press: -1 to Exit");

    response = kb.nextInt();
    switch (response) 
       {
            case 1:
                there is code here-
            case 3:
                there is code here-
            case 4:
                there is code here-
            case 5:
                there is code here-
            case 6:
                there is code here-
            case -1:
                System.out.println("Exiting program");
                break;
            default:
                System.out.println("Not understood. Re-enter");
                break;
        }

    }while (response != -1);

确保它位于Do-While循环的末尾,而不是像以前一样。

答案 2 :(得分:0)

我在剩下的剩菜中得到了这个

将开关转换为if-else块。

给出“孤立的默认”错误的最小示例:

  public static void whatever(){
      if( true ){      
          default: return; 
      };;
  };;