0

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");
        }
  • Your formatting is horrible. You need to indent your code so it's easier to understand. Also, you might want to look into learning how to create your own methods and objects. Having all your code in main() isn't a good idea. – u3l Feb 19 '14 at 03:57
  • I can't have any other methods... –  Feb 19 '14 at 04:02
  • What's wrong is my formatting? I'm a noob in programming. –  Feb 19 '14 at 04:06
  • Indentation. after every if statement or `for`/`while` loop, you need to indent your code by ~4 spaces. It makes it easier to read. – u3l Feb 19 '14 at 04:07
  • First off only give us the code you are having a problem with. Pasting your whole application in the question is only going to make it difficult to help you. Secondly All this white space makes it impossible to figure out what is going on in your code without having a photographic memory. Not to beat you down but you got to give us some more effort so we can help you out better. Don't get discouraged just fix it and come back. This is a community that wants to help you. – 137 Feb 19 '14 at 04:23
  • If you are not familiar with what code formatting is, checkout http://prettyprinter.de – Can't Tell Feb 19 '14 at 04:29
  • Fixed your coin toss issue. – 137 Feb 19 '14 at 04:48
  • 1
    The only issue is that the number of coin tosses are additive instead of independent. Also the 0 doesn't redirect to the main menu, have any ideas? For example, COIN TOSS SIMULATOR Enter 0 to quit. How many tosses? 3 2.0 heads and 1.0 tails means 66.66666666666666% were heads Enter 0 to quit. How many tosses? 3 4.0 heads and 2.0 tails means 66.66666666666666% were heads Enter 0 to quit. How many tosses? 0 –  Feb 19 '14 at 05:12
  • Fixed coin toss by resetting values at the end of the loop.r – 137 Feb 19 '14 at 05:21
  • Thanks man, I've learned a lot from you. Should I put a continue statement to redirect to the main menu after the user enters 0? –  Feb 19 '14 at 05:30

1 Answers1

1

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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
137
  • 781
  • 1
  • 8
  • 21