繁体   English   中英

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

[英]Java - How to contain a switch statement menu in a while loop

我有一个主菜单类,它从用户那里得到选择,然后使用该选择从与菜单选项有关的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)
}

情况是'Exit'选项,因此我将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 side = 0; private int dieOne; private int die2;

    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,2和3中删除return 。如果从main return ,程序将终止。 您想循环播放,所以不要那样做。 但是,正如@ajb在以下评论中指出的那样,您不希望这种情况发生。 因此,您需要break

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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