gpt4 book ai didi

Java 菜单循环无限重复

转载 作者:行者123 更新时间:2023-12-02 06:40:39 25 4
gpt4 key购买 nike

这是我的程序的主类。

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

这是“方法”类。

    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

如果我选择一个选项,例如 3,该选项将永远重复(直到我关闭命令行)。我周二缺课了(感谢你没有闹钟响!),因此错过了我们关于循环的讲座。 :/如果有人可以提供一些帮助(也许是一些指示?),我们将不胜感激!我想要的只是选择一个选项,执行该方法中的操作,然后返回菜单选择另一个方法,直到用户选择“9”并退出。如果以后我也需要摆脱那个可怕的嵌套。

最佳答案

option = seu03Methods.seu03Menu(); 移动到 while 循环中(向下两行进入 while(option != 9){..。)你永远不会改变选项在循环中,所以它总是三个

关于Java 菜单循环无限重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19169493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com