Switch语句不退出循环。 (Java)的

时间:2016-03-22 17:27:21

标签: java loops switch-statement do-while

我设置了一个简单的菜单系统,用户点击8离开。但是出于某种原因,当我在测试过程中遇到8时,它只是回到了循环的顶端,就像没有发生任何事情一样。

package potluck;
import java.util.*;

import potluck.*;

public class Controller {
private Scanner input;


private final static int USER_LOGIN = 0;
private final static int CREATE_MEMBER = 1;
private final static int CREATE_ADMIN = 2;
private final static int CREATE_RECIPE = 3;
private final static int COMMENT = 4;
private final static int DELETE_RECIPE = 5;
private final static int EXIT = 8;

public Controller(){
    input = new Scanner(System.in);
    startUp();//no better name to be thought of
}

public void startUp() {
    // TODO Auto-generated method stub
    int choice;
    do {
        this.displayMenu();
        choice = input.nextInt();
        input.nextLine();// clears carriage return

        //depending on choice takes to a different menu
        switch (choice) {

        case CREATE_MEMBER: 
            Member member = new Member();
            break;
//     case CREATE_ADMIN: 
//      member.addAdmin();
//              break;
        case CREATE_RECIPE:
            Recipe.addRecipe();
            break;
        case COMMENT:
            Recipe.addComment();
            break;
        case DELETE_RECIPE:
            Recipe.deleteRecipe();
            break;
        case EXIT:
            System.out.println("Thanks for using our software");
            break;
        default:
            System.out.println("Error, Invalid selection.");
        }
    } while (choice != 8); //choice 8 exits
}
private void displayMenu() {
    System.out.println("1 Create Member");
    System.out.println("2 Create Admin Member");
    System.out.println("3 Create Recipe");
    System.out.println("4 Leave Comment");
    System.out.println("5 Delete Recipe");
    System.out.println("8 Exit");
    System.out.println("Please enter menu option, to exit enter 8");
    }
}

在测试中,它声称选择是8,这应该打破做的同时......但不是......

更新:当复制代码时,我离开了一些工作,我被告知不要使用。在选择8下我有system.exit,但被告知这是错误的代码

1 个答案:

答案 0 :(得分:0)

对不起大家。我是个白痴。在我启动它的主要部分,我已经实例化了一个新的控制器(这是你正在查看的类),它在构造函数中启动菜单。然后我调用了startup()方法。这意味着它工作得很好,它只是跑了两次,因为我很蠢。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Controller cntlr = new Controller();    
    cntlr.startUp();
}


public Controller(){
    input = new Scanner(System.in);
    startUp();//no better name to be thought of
}

public void startUp() {
    // TODO Auto-generated method stub
    int choice;
    do {
        this.displayMenu();
        choice = input.nextInt();
        input.nextLine();// clears carriage return

        //depending on choice takes to a different menu
        switch (choice) {

        case CREATE_MEMBER: 
            Member member = new Member();
            break;
        case CREATE_ADMIN: 
            member.addAdmin();
            break;
        case CREATE_RECIPE:
            Recipe.addRecipe();
            break;
        case COMMENT:
            Recipe.addComment();
            break;
        case DELETE_RECIPE:
            Recipe.deleteRecipe();
            break;
        case EXIT:
            System.out.println("Thanks for using our software");

            break;
        default:
            System.out.println("Error, Invalid selection.");
        }
    } while (choice != 8); //choice 8 exits
}
private void displayMenu() {
    System.out.println("1 Create Member");
    System.out.println("2 Create Admin Member");
    System.out.println("3 Create Recipe");
    System.out.println("4 Leave Comment");
    System.out.println("5 Delete Recipe");
    System.out.println("8 Exit");
    System.out.println("Please enter menu option, to exit enter 8");
    }
}

TL; DR。我很笨,施工人员应该多加注意。