如何根据用户的输入使这些循环继续?在java eclipse中



当我运行此代码时,它是一个具有许多不同选项的菜单。它由许多循环组成。其中一些我还没有制作。我的掷硬币模拟器有问题。虽然我有一个for循环,但循环只循环一次,而不是直到用户选择"0"退出程序并返回主菜单,但我的另一个问题是,如果选择0,模拟器将不会返回主菜单。发生了什么事???这是我烦恼的一个例子。

输入种子值:3

=====CS302工具箱=====

掷硬币模拟器

G>等级估计器

颜色挑战

Q>退出键入您选择的代码字母:t

硬币投掷模拟器

输入0退出。投掷了多少次?4

3.0头和1.0尾意味着75.0%是头

然后模拟器不会再次询问"输入0退出。投掷了多少次?"。事实上,如果我再按一个数字,比如3,我就会得到这个。零也不会结束模拟器并再次提示主菜单。

3不是一个有效的选择。

=====CS302工具箱=====

掷硬币模拟器

G>等级估计器

颜色挑战

Q>退出

System.out.println("");
System.out.println( "===== CS302 TOOL BOX =====nT > COIN TOSS SIMULATORnG > GRADE ESTIMATORnC > COLOR CHALLENGEnQ > QUIT");
{        
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c;
while (!usersSelection)
{
{
System.out.print(""+ "Type code letter for your choice: ");
}


if (anotherScanner.hasNext("q|Q"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println(""
+ ""
+ "Good-Bye");
break;
}



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 headsvsTails;
headsvsTails = rand.nextBoolean();
for (int i =0; i < feeble; i++)
{
headsvsTails = rand.nextBoolean();
if (headsvsTails == true){ heads++;}
else {tails++;}
} 
System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
}






if  (anotherScanner.hasNext("g|G")) 
{
c = anotherScanner.next();
usersSelection = true;
System.out.println("");
System.out.println("GRADE ESTIMATOR");
Scanner gradeE = new Scanner(System.in);
double exam1;
double possiblePts;
double programPts;
double programPossible;
double avg;
double avge;
double avrg;


System.out.print("Enter exam points earned (int): ");
exam1=gradeE.nextDouble();
if (exam1<0)
{System.out.print("Enter exam points earned (int): ");
exam1=gradeE.nextDouble();}
System.out.print("Enter exam points possible (int): ");
possiblePts=gradeE.nextDouble();
if (possiblePts<0) {System.out.print("Enter exam points possible (int): ");
possiblePts=gradeE.nextDouble();}
System.out.print("Enter program points earned (int): ");
programPts=gradeE.nextDouble();

if (programPts<0) {System.out.print("Enter program points earned (int): ");
programPts=gradeE.nextDouble();}
System.out.print("Enter program points possible (int): ");
programPossible=gradeE.nextDouble();
if (programPossible<0) {System.out.print("Enter program points possible (int): ");
programPossible=gradeE.nextDouble();}

avge = exam1+programPts;
avrg = possiblePts+programPossible;
avg = avge/avrg*100;
if (avg<60)
System.out.println("Grade estimate for " +avg+ "% is a F");
else if (avg<70)
System.out.println("Grade estimate for " +avg+ "% is a D");
else if (avg<80)
System.out.println("Grade estimate for " +avg+ "% is a C");
else if (avg<90)
System.out.println("Grade estimate for " +avg+ "% is a B");
else if (avg>=90)
System.out.println("Grade estimate for " +avg+ "% is a A");
}

好吧,伙计,你的代码有很多错误。您不断申报新的扫描仪。间距不一致,代码的各个部分被太平洋跨度的空白海洋隔开。

这里有一个修复:

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 headsvsTails;
headsvsTails = rand.nextBoolean(); // This is Worthless
while ( feeble != 0 ) { //Pay attention to this while loop
for (int i =0; i < feeble; i++) {
headsvsTails = rand.nextBoolean();
if (headsvsTails == 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
}
}

这里的关键是while循环。我用你的弱变量作为它的条件。只要用户不提供0,它就会运行。

请花点时间引导朋友或教授浏览您的代码。向他们解释,鼓励他们提出问题。

此外,我认为必须阅读这篇文章。不要气馁。当我看到我3年前写的代码时,它看起来很像这样。你会好起来的。

*固定代码,因此头/尾不会根据注释的输入进行添加。

相关内容

  • 没有找到相关文章