[英]How to have my program return to the main menu?
在第二個while循環中(在此人選擇了拋硬幣模擬器選項之后),當用戶選擇0時,我遇到了問題,程序沒有按我希望的那樣退回到主菜單,而是停止了,並且沒有請勿循環回到主菜單,以供用戶選擇其他選項。
任何想法如何解決這一問題? 我不能使用多種方法,因為這是我正在做的項目的要求。 我(一個星期)已經在這部分上停留了很長時間,並且非常感謝任何指示或指導。
這是我的程序在用戶運行拋硬幣模擬器后選擇零時的樣子。
===== CS302工具箱=====
T>投幣模擬器
G>等級估算器
C>色彩挑戰
問>退出
輸入代碼字母供您選擇:t
投幣模擬器
輸入0退出。 扔多少次?
4
2.0的正面和2.0的反面意味着50.0%是正面
輸入0退出。 扔多少次?
3
3.0的正面和0.0的反面意味着100.0%是正面
輸入0退出。 扔多少次?
0
(只是停止轉而退回到主菜單時)
{
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
outer:
while (!usersSelection)
{
System.out.println("===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");
String c;
System.out.print(""+ "Type code letter for your choice: ");
if (anotherScanner.hasNext("q|Q"))
{
c = anotherScanner.next();
//usersSelection = true;
System.out.println("\n Good-bye");
break;
}
else if (anotherScanner.hasNext("t|T")){
c = anotherScanner.next();
usersSelection = true;
System.out.println("");
System.out.println("COIN TOSS SIMULATOR");
System.out.println("");
System.out.println("Enter 0 to quit. How many tosses?");
Random rand = new Random();
Scanner insideScanner = new Scanner(System.in);
int feeble = insideScanner.nextInt();
double heads = 0;
double tails = 0;
boolean hvt;
while ( feeble != 0 ) { //Pay attention to this while loop
if (feeble == 0){break outer;}
for (int i =0; i < feeble; i++) {
hvt = rand.nextBoolean();
if (hvt == true){ heads++;}
else {tails++;}
}
System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
System.out.println("Enter 0 to quit. How many tosses?"); //I ask the question again
heads = 0;
tails = 0;
feeble = insideScanner.nextInt();//I get new input
}
}
usersSelection
仍然為true,因此外部循環( while(!usersSelection)
)不再運行。
同樣, if (feeble == 0){break outer;}
是多余的,即使它確實起作用,也可以使用break outer;
將結束程序(通過退出外部循環)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.