Java - 如何在while循环中包含switch语句菜单

时间:2015-11-18 04:40:30

标签: java menu while-loop switch-statement

我有一个主菜单类,可以从用户那里选择,然后使用该选项从与菜单选项相关的switch语句中选择其他类。我的代码是:

public static void main(String[] args) {
    int dieOne = 0;
    int dieTwo = 0;
    int choice = 0;

    DiceMaker dice = new DiceMaker(); // class that creates the dice
    RollDice roll = new RollDice(); // class that imitates roll
    DiceMenu menu = new DiceMenu();
    DiceRoller series = new DiceRoller();

    System.out.println("Welcome to the Dice Roll Stats Calculator!\n");
    while (choice != 4) {
        menu.DiceMenu();
        choice = menu.getUserChoice();

        switch (choice) {
        case 1:
            dice.diceMaker();
            dieOne = dice.DieOne();
            dieTwo = dice.DieTwo();
            System.out.println(dice.DieOne() + dice.DieTwo());
            return;
        case 2:
            roll.rollDice(dieOne, dieTwo);
            roll.displayRoll();
            return;
        case 3:
            series.diceRoller();
            series.displayResults();
            return;
        case 4:
            break;
        }// switch (choice)

    } // while (choice != 4)
}

案例是退出'选项,所以我把switch语句放在一个while循环中,布尔条件不等于4,这样当选择设置为4时,循环就会停止。正确的案例执行但我遇到的问题是循环,因此程序在我尝试的每个案例后停止,即使选择不是4.我尝试在案例1,2和3之后使用break语句同样,当我这样做时,它只会在无限循环中重复这个案例。我试图在我自己的切口上弄清楚这一点,我找不到任何类似我看到的东西,足以让我弄清问题是什么。我猜这可能不是将来制作菜单的最佳方式。提前谢谢。

我的其余代码如下。请注意,DiceRoller类仍在构建中,但DiceMaker和RollDice类似乎正在运行。

DiceMenu课程:

public class DiceMenu 
{
    public static final int CHOICE_UNKNOWN = 0;
    public static final int CHOICE_MAKE_DICE = 1;
    public static final int CHOICE_ROLL_ONCE = 2;
    public static final int CHOICE_SERIES_ROLL = 3;
    public static final int CHOICE_QUIT = 4;

    private int choice = 0;
    Scanner scan = new Scanner(System.in);

    public int DiceMenu()
    {

        while ( this.choice < 1 || this.choice > 4 ) // while loop keeps choices in range
        {   
            System.out.println("         MAIN MENU\n");
            System.out.println("1. Create Your Dice");
            System.out.println("2. Roll Your Dice");
            System.out.println("3. Perform A Series Of Rolls And Show Stats");
            System.out.println("4. Exit\n");

            try // avoid invalid input 
            { 
                System.out.print("Please choose an option: ");
                this.choice = scan.nextInt(); // get number of sides from user
            } 
            catch (InputMismatchException e) 
            {
            //if input is invalid, returns to beginning of loop    
            System.out.println("Invalid Input. Please try again.\n");
            scan.next();
            continue;   
            }

            if ( this.choice < 1 || this.choice > 4 ) // if input is out of range
                                     // notify user before continuing
            {
                System.out.println("Choice must reflect menu options. (1-4)"
                             + " Please try again.\n");
                this.choice = 0;
            }

        }//while ( this.choice < 1 || this.choice > 4 )                 
        return 0;
    }        
    public int getUserChoice()
    {
        return this.choice;
    }       
}

RollDice类:

public class RollDice 
{
    private int roll;
    private int rollOne;
    private int rollTwo;
    private int rollTotal;

    public int rollDice (int dieOne, int dieTwo) 
    {
        this.rollOne = 1 + (int)(Math.random() * dieOne);
        this.rollTwo = 1 + (int)(Math.random() * dieTwo);
        this.rollTotal = this.rollOne + this.rollTwo;
        return 0;
    }

    public void displayRoll()
    {
        System.out.println("You roll a " + rollOne + " and a "
                       + rollTwo + " for a total of " + 
                       rollTotal + "!"); //display separate and total 
                                         //roll amounts

        if ( rollTotal == 2 ) // if/else tests for special rolls
        {
            System.out.println("Snake Eyes!");
        } 
        else if ( rollTotal == 7 )
        {
            System.out.println("Craps!");
        }
        else if ( rollOne == 6 && rollTwo == 6 )
        {
            System.out.println("Boxcars!");
        }
    }
}// public class DiceRoller

DiceMaker类:     公共课DiceMaker     {         private int sides = 0;         private int dieOne;         private int dieTwo;

    public int diceMaker() 
    {
        while ( sides < 4 || sides > 20 ) // while loop keeps sides within        range
        {
            Scanner scan = new Scanner(System.in);

            try // avoid invalid input 
            { 
                System.out.print("Please enter the number of sides each die "
                +            "should have (must be between 4 and 20): ");
                this.sides = scan.nextInt(); // get number of sides from user
            } 
            catch (InputMismatchException e) 
            {
            //if input is invalid, returns to beginning of loop    
            System.out.println("Invalid Input. Please try again.\n");
            scan.next();
            continue;   
            }
            if (sides < 4 || sides > 20) // if input is out of range
                                     // notify user before continuing
            {
                System.out.println("Die must have between 4 and 20 sides."
                             + " Please try again.\n");
            }
        }//while ( sides < 4 || sides > 20 )                    

        this.dieOne = sides;
        this.dieTwo = sides;
        return 0;
    }

    public int DieOne()
    {
        return this.dieOne;
    }

    public int DieTwo()
    {
        return this.dieTwo;
    }


}// public class DiceMaker 

1 个答案:

答案 0 :(得分:1)

从案例1,2和3中删除return。如果return来自main,程序将终止。你想循环,所以不要这样做。但是,正如@ajb在下面的评论中指出的那样,您并不希望案件落空。所以你需要break(s)。

case 1: dice.diceMaker();
        dieOne = dice.DieOne();
        dieTwo = dice.DieTwo();
        System.out.println(dieOne + dieTwo);
        // return;
        break; // <-- applies to innermost block (switch).
case 2: roll.rollDice(dieOne, dieTwo);
        roll.displayRoll();
        // return;
        break; // <-- applies to innermost block (switch).
case 3: series.diceRoller();
        series.displayResults();
        // return;
        break; // <-- applies to innermost block (switch).

另外,你可以使用continue(这里,它适用于最里面的循环)。最后,请记住案例4会终止循环(因为choice4),因此您不需要case 4