简体   繁体   English

Java菜单循环无限重复

[英]Java Menu Loop Repeats Infinitely

So here is my main class for the program. 这是该程序的主要课程。

import java.util.Scanner;
import java.util.Random;
public class seu03
{
public static void main (String[] args)
{
int option = 0;
option = seu03Methods.seu03Menu();
    while(option != 9)
        {
switch (option)
    {
    case 1:
        seu02.calculator();
        break;
    case 2:
        seu03Methods.sentenceAnalysis();
        break;
    case 3:
        seu03Methods.rollDice();
        break;
    case 4:
        seu03Methods.bigMoney();
        break;
    case 9:
        System.out.println("Exiting seu03...");
        System.exit(0);
        break;
    default:
        System.out.println("Invalid selection.");
        break;
    }
            }
} //end of main
} //end of class

And this is the "methods" class. 这就是“方法”类。

    import java.util.Scanner;
    import java.util.Random;
    public class seu03Methods
{
    public static int seu03Menu()
    {
    int option = 0;
    Scanner console = new Scanner(System.in);   
    System.out.println("-------------------------------------");
    System.out.println("|         Welcome to seu03!         |");
    System.out.println("|-----------------------------------|");
    System.out.println("| Choose an option below:           |");
    System.out.println("|                                   |");
    System.out.println("| 1.  Calculator                    |");
    System.out.println("| 2.  Sentence Analysis             |");
    System.out.println("| 3.  Roll a 6-sided die            |");
    System.out.println("| 4.  Play Big Money!               |");
    //System.out.println("| 5. randomRange            |");
    //System.out.println("| 6. stubMethod            |");
    System.out.println("| 9.  Exit                          |");
    System.out.println("-------------------------------------");
    option = console.nextInt();
    return option;
    }
    public static void sentenceAnalysis()
    {
    Scanner console = new Scanner(System.in);
    String userString = new String("");
    System.out.println("-------------------------------------");
    System.out.println("Please enter a sentence for analysis:");
    userString = console.nextLine();
    System.out.println("The sentence to analyze is: " + userString);
    System.out.println("The sentence is " + userString.length() + " characters long.");
    System.out.println("The first character of the sentence is " + userString.charAt(0));
    System.out.println("The middle charchter of the sentence is " + userString.charAt(userString.length()/2));
    System.out.println("The last character of the sentence is " + userString.charAt(userString.length()-1));
    System.out.println("Thanks for using our 1323 System!");
    System.out.println("-------------------------------------");
    return;
    } //end of sentenceAnalysis
    public static void rollDice()
    {
    Random gener = new Random();
    int die = 0;
    System.out.println("-------------------------------------");
    System.out.println("Welcome to Roll Dice!");
    System.out.println("The program will now roll a single six-sided die.");
    die = gener.nextInt(6) + 1;
    System.out.println("Result: " + die);
    System.out.println("Thanks for using our 1323 System!");
    System.out.println("-------------------------------------");
    return;
    } //end of rollDice
    public static void bigMoney()
    {
    Scanner console = new Scanner(System.in);
    Random gener = new Random();
    int rnum = 0;
    int guess1 = 0;
    int guess2 = 0;
    int guess3 = 0;
    System.out.println("-------------------------------------");
    System.out.println("Welcome to Big Money!");
    rnum = gener.nextInt(10) + 1;
    System.out.println("A whole number between 1 and 10 inclusive has been generated. You will have three chances to guess the number.");
    System.out.println("Here is the random number for testing purposes: " + rnum);
    System.out.println("What is your first guess?");
    guess1 = console.nextInt();
    if(guess1 == rnum)
        {
        System.out.println(" Big Money! You win $100!");
        }
    else
        {
        System.out.println("No good! What is your second guess?");
        guess2 = console.nextInt();
            if(guess2 == rnum)
            {
            System.out.println("Medium Money! You win $50!");
            }
            else
                {
                System.out.println("No good! What is your third guess?");
                guess3 = console.nextInt();
                    if(guess3 == rnum)
                    {
                    System.out.println("Little Money! You win $1!");
                    }
                        else
                        {
                        System.out.println("Sorry! You didn't win any money this time. Better luck next time!");
                        }
                }
            }   
    System.out.println("Thanks for playing Big Money!");
    System.out.println("Thanks for using our 1323 System!");
    System.out.println("-------------------------------------");
    return;
    } //end of bigMoney
} //end of class

If I choose an option, say, 3, that option will repeat forever (until I close the command line). 如果我选择一个选项,例如3,则该选项将永远重复(直到我关闭命令行)。 I missed class Tuesday (thanks for not going off alarm!) and thus missed our lecture on loops. 我错过了星期二的课堂(感谢您不要发出警报!),因此错过了我们关于循环的讲座。 :/ If anyone could provide some assistance (maybe some pointers?) that would be greatly appreciated! ://如果有人可以提供一些帮助(也许有一些指示?),将不胜感激! All I want is to choose an option, do what is in that method, then return to the menu to choose another method until the user selects "9" and exits. 我只想选择一个选项,执行该方法中的操作,然后返回菜单以选择另一种方法,直到用户选择“ 9”并退出。 And I need to get rid of that horrid nested if later too. 而且,如果以后也需要摆脱那套可怕的嵌套。

move option = seu03Methods.seu03Menu(); 移动option = seu03Methods.seu03Menu(); into the while loop( down two lines into while(option != 9){.. .) You never change option in the loop it so it is always three 进入while循环(向下两行进入while(option != 9){.. )您永远不会在循环中更改option ,因此它总是3

while(option != 9)
        {
switch (option)
    {
    case 1:
        seu02.calculator();
        break;
    case 2:
        seu03Methods.sentenceAnalysis();
        break;
    case 3:
        seu03Methods.rollDice();
        break;
    case 4:
        seu03Methods.bigMoney();
        break;
    case 9:
        System.out.println("Exiting seu03...");
        System.exit(0);
        break;
    default:
        System.out.println("Invalid selection.");
        break;
    }
            } 

you can never get to your case : 9, to exit....because the while loop is telling it to skip the switch if the int is 9 ( which i assume would close the app) try changing this 您永远无法解决您的问题:9,退出....因为while循环告诉它如果int为9(我认为会关闭应用程序)则跳过开关,请尝试更改此设置

while(option != 9)

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

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