在第二个while循环中(在用户选择硬币投掷模拟器选项之后)我遇到问题当用户选择0时程序没有按照我的意愿退回到主菜单,而只是停止,并且不会循环返回主菜单以供用户选择其他选项。
任何想法如何解决这个问题?我不能使用多种方法,因为这是我正在做的项目的要求。我已经被困在这一部分很长一段时间了,(一周)并且会感激地欣赏任何指针或方向。
这是我的程序在运行硬币抛掷模拟器后用户选择零时的样子。
===== CS302 TOOL BOX =====
T> COIN TOSS SIMULATOR
G>等级估计
C>色彩挑战
Q> QUIT
输入您选择的代码字母:t
COIN TOSS SIMULATOR
输入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
}
}
答案 0 :(得分:0)
usersSelection
仍然是正确的,因此外循环(while(!usersSelection)
)不会再次运行。
此外,if (feeble == 0){break outer;}
是多余的,即使它确实有效,break outer;
也会结束程序(通过退出外循环)。