有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个主菜单类,它从用户那里获得一个选项,然后使用该选项从与菜单选项相关的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类似乎正在工作

菜单类:

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

骰子制造者等级: 公营制骰机 { 私有int边=0; 私营企业; 私人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) 个答案

  1. # 1 楼答案

    从案例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