简体   繁体   中英

Switch Statement not exiting loop. (Java)

I have a simple menu system set up where the user hits 8 to leave. However for some reason when i hit 8 during testing it simply goes back to the top of the loop like nothing happened.

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");
    }
}

In testing it claims that choice is 8, which should break the do while...but doesn't...

UPDATE: When copying over code, I left in some work around that i was told not to use. I had system.exit in there under choice 8, but was told that's bad code

Sorry everyone. I'm an idiot. In the main where I launch this, I had instantiated a new controller (which is the class you're looking at) which in the constructor launches the menu. Then i called the startup() method. Which means that it was working perfectly, it was just running twice because i'm dumb.

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. I'm dumb, and constructors are a thing I should pay more attention to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM