Reputation:
When I run this code, which is a menu with many different options. it consists of many loops. Some of which I have yet to make. I am having trouble with my coin toss simulator. Although I have a for loop, the loop only cycles through once instead of until the user selects "0" to exit the program and return to the main menu but also my other problem is that the simulator wont return to the main menu if 0 is selected. What is going on??? here is an example of my troubles.
Enter seed value: 3
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT Type code letter for your choice: t
COIN TOSS SIMULATOR
Enter 0 to quit. How many tosses? 4
3.0 heads and 1.0 tails means 75.0% were heads
Then the simulator doesn't ask "enter 0 to quit. How many tosses?" again. in fact if I press another number, say 3 I'd get this. Also zero doesn't end the simulator and prompt the main menu again.
3 is not a valid choice.
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT
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");
}
Upvotes: 0
Views: 1355
Reputation: 831
Alright buddy there is a lot wrong with your code. You constantly declare new Scanners. Spacing is inconsistent and portions of your code are separated from each other by a sea of white space the span of the Pacific Ocean.
Here is a fix:
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
}
}
The key here is the while loop. I use your feeble variable as its condition. It will run so long as the user does not provide a 0.
Please take some time to walk a friend or a professor through your code. Explain it to them, encourage them to ask questions.
Also I consider it mandatory to read this. Don't be discouraged. When I look at the code I wrote 3 years ago it looked a lot like this. You will get better.
*fixed code so heads/tails is not additive based on input from comments.
Upvotes: 1