![](/img/trans.png)
[英]how can I make this coin flip simulation work based on user input? java eclipse
[英]how can I make these loops continue based on users input? In java eclipse
当我运行此代码时,这是一个包含许多不同选项的菜单。 它包含许多循环。 其中一些我还没有做。 我的抛硬币模拟器遇到问题。 尽管我有一个for循环,但循环只循环一次,而不是直到用户选择“ 0”退出程序并返回主菜单为止,但是我的另一个问题是,如果0为0,模拟器将不会返回主菜单选择。 到底是怎么回事??? 这是我遇到麻烦的一个例子。
输入种子值:3
===== CS302工具箱=====
T>投币模拟器
G>等级估算器
C>色彩挑战
Q> QUIT键入代码字母供您选择:t
投币模拟器
输入0退出。 扔多少次? 4
3.0的正面和1.0的反面意味着75.0%是正面
然后模拟器不会要求“输入0退出。要扔多少次?” 再次。 事实上,如果我按另一个数字,说3我会得到这个。 同样,零不会结束模拟器并再次提示主菜单。
3不是有效的选择。
===== CS302工具箱=====
T>投币模拟器
G>等级估算器
C>色彩挑战
问>退出
System.out.println("");
System.out.println( "===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > 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,它将一直运行。
请花一些时间陪伴朋友或教授讲解您的代码。 向他们解释,鼓励他们提出问题。
我也认为阅读本文是强制性的。 不要气our。 当我查看三年前编写的代码时,它看起来很像这样。 你会好起来的。
*固定代码,因此根据注释的输入,正面/反面不会加法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.