Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Skipping parts of if loop (Coin toss Program)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Skipping parts of if loop (Coin toss Program)

    Hey guys, here's my code:
    /*
    Paulino Rosado
    10/3/2012
    Computer statistics for eight coin tosses
    */
    import java.util.*;
     
    class Ch4Pa2
    {
    	public static void main(String[] args)
    	{
    		int heads=0, tails=0;
    		double percentageHeads, percentageTails;
    		char tossresult;
    		String tempstring;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("For each coin toss enter either h for heads or t for tails");
     
    		for(int i=1; i<=8; i++)
    		{
    			System.out.println("Toss: "+i);
    			tempstring = keyboard.next();
    			tossresult = tempstring.charAt(0);
     
    			if(tossresult == 'h')
    			{
    				i = i+1;
    				heads = heads + 1;
    			}
    			else if(tossresult == 't')
    			{
    				i = i+1;
    				tails = tails + 1;
    			}
    			else
    			{
    				i= i-1;
    				System.out.println("Incorrect toss.  You must enter either h or t");
    			}
    		}
     
    			System.out.println("Number of heads: "+heads);
    			System.out.println("Number of tails: "+tails);
    			percentageHeads =(double)heads/8;
    			percentageTails = (double)tails/8;
    			System.out.println("Percent heads: "+percentageHeads);
    			System.out.println("Percent tails: " +percentageTails);
    	}
    }
    The problem is this is the output:
    For each coin toss enter either h for heads or t for tails
    Toss: 1
    h
    Toss: 3
    b
    Incorrect toss.  You must enter either h or t
    Toss: 3
    a
    Incorrect toss.  You must enter either h or t
    Toss: 3
    t
    Toss: 5
    t
    Toss: 7
    h
    Number of heads: 2
    Number of tails: 2
    Percent heads: 0.25
    Percent tails: 0.25
    Press any key to continue . . .
    As you can see, it's only going through the loop 4 times instead of 2! It is counting in 2s, and I don't really understand why :/


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Skipping parts of if loop (Coin toss Program)

    Quote Originally Posted by ChicoTheMan94 View Post
    ...
    As you can see, it's only going through the loop 4 times instead of 2! It is counting in 2s, and I don't really understand why :/
    If you have really studied the code and don't understand how the program produces the output that you are seeing, here's a suggestion:

    Why don't you "play like" you are the computer?

    How's how to play:

    Get a pencil and some paper. Really. Pencil. Paper. (You remember those, right?)

    Write down initial values of all variables that are defined. Whenever there is an assignment statement that changes the value of a particular variable, cross out the old value and write down the now value.

    Now, let's go:
    Start at the top of the loop. Write down the value of the variable i

    Now, pretend that the user enters an 'h' from the keyboard.

    Follow the steps that the program will take. Write down changes that the program will make to any variable that gets a new value.

    When you get to the end of the loop (on paper) go back to the top of the loop.

    Write down the value of i.

    Repeat until you "get it."

    If you don't "get it," post again. Present the steps that you followed in your pretend-I'm-the-computer game.


    Cheers!

    Z
    Last edited by Zaphod_b; October 5th, 2012 at 03:40 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ChicoTheMan94 (October 5th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Skipping parts of if loop (Coin toss Program)

    it's only going through the loop 4 times instead of 2
    The code inside of the loop changes the value of the loop's index variable: i. That will change how many times the loop will be executed. Try debugging the code by adding println statements to show the values of variables used in the loop so you can see what the computer sees.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ChicoTheMan94 (October 5th, 2012)

  6. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Skipping parts of if loop (Coin toss Program)

    Omg! I got it, thanks to you both. Why I added i++ to my if statements I'll never know, no duh it was adding an extra i, I did it manually! Face palming incredibly hard, feel like maybe someone drugged me before I coded this. Thanks!

    Here is the completed code, works perfect!
    /*
    Paulino Rosado
    10/3/2012
    Computer statistics for eight coin tosses
    */
    import java.util.*;
     
    class Ch4Pa2
    {
        public static void main(String[] args)
        {
            int heads=0, tails=0;
            double percentageHeads, percentageTails;
            char tossresult;
            String tempstring;
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("For each coin toss enter either h for heads or t for tails");
     
            for(int i=1; i<=8; i++)
            {
                System.out.println("Toss "+i+": ");
                tempstring = keyboard.next();
                tossresult = tempstring.charAt(0);
     
                if(tossresult == 'h')
                {
                    heads = heads + 1;
                }
                else if(tossresult == 't')
                {
                    tails = tails + 1;
                }
                else
                {
                    i--;
                    System.out.println("Incorrect toss.  You must enter either h or t");
                }
            }
     
                System.out.println("Number of heads: "+heads);
                System.out.println("Number of tails: "+tails);
                percentageHeads = ((double)heads/8) * 100;
                percentageTails = ((double)tails/8) * 100;
                System.out.println("Percent heads: "+percentageHeads+"%");
                System.out.println("Percent tails: " +percentageTails+"%");
        }
    }
    Last edited by ChicoTheMan94; October 5th, 2012 at 03:55 PM.

Similar Threads

  1. [SOLVED] My program skipping a for loop
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2012, 09:05 AM
  2. Making Coin Toss for Football game
    By tyb97 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 2nd, 2011, 02:00 PM
  3. [SOLVED] Java Noob: Coin Toss Simulator (no GUI)
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 4th, 2011, 10:28 PM
  4. Repeating different parts of a program
    By swiftxjames in forum Loops & Control Statements
    Replies: 3
    Last Post: November 17th, 2010, 03:29 PM
  5. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 03:14 AM