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 6 of 6

Thread: Java Programming Exercise

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Programming Exercise

    Hello fellow java programmers, I need help with an exercise found within this link (Exercise 3.1) Javanotes 6.0, Excercises for Chapter 3

    Here's my code:
    import java.util.Random;
    public class ExerciseThreeOne {
     
    	public static void main(String[] args){
     
    		Random firstDiceRolls = new Random();
    		Random secondDiceRolls = new Random();
    		int runningTotal = 0;
    		boolean snakeEyes = false;
     
    		do {
     
    			int firstDice = firstDiceRolls.nextInt(6) + 1;
    			int secondDice = secondDiceRolls.nextInt(6) + 1;
     
     
    			if ( (firstDice == 1) && (secondDice == 1) ) {
     
    				snakeEyes = true;
    			}
    				else
    					snakeEyes = false;
    					runningTotal += 1;
     
    		} while (snakeEyes = false);
     
    		System.out.println("It took " + runningTotal + " time(s) to receive snake eyes.");
    		}
     
    }

    For some reason, the program keeps saying that it took 1 time to generate snake eyes.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Java Programming Exercise

    Always, ALWAYS, enclose all blocks in curly braces, even one line blocks. This includes all if blocks and else blocks.

    Doing this will solve your problem.

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

    Default Re: Java Programming Exercise

    Quote Originally Posted by Garron5899 View Post
    ...
    Here's my code:
    ...
    For some reason, the program keeps saying that it took 1 time to generate snake eyes.
    If it says that every time, then there are a couple of possibilities:

    Either it is not counting correctly, or it is not looping correctly, or the logic that gets the dice values is incorrect. (Note that more than thing might be incorrect.)

    Question: How can we know where the bad behavior comes from?

    Answer: Make the program tell us.

    Put some print statements to show exactly what the program is working on. (I reformatted the spacing and indentation slightly, but the important point is the print statements that show the flow and the values.)
            do
            {
                int firstDice = firstDiceRolls.nextInt(6) + 1;
                int secondDice = secondDiceRolls.nextInt(6) + 1;
                System.out.println("firstDie = " + firstDice + ", secondDie = " + secondDice);
     
                if ( (firstDice == 1) && (secondDice == 1) )
                {
                    snakeEyes = true;
                }
                else
                    snakeEyes = false;
     
                runningTotal += 1;
     
                System.out.println("Bottom of loop: snakeEyes = " + snakeEyes + ", running total = " + runningTotal);
            } while (snakeEyes = false);



    Cheers!

    Z
    Last edited by Zaphod_b; July 20th, 2012 at 01:46 AM.

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

    Default Re: Java Programming Exercise

    Quote Originally Posted by Fubarable View Post
    ..enclose all blocks in curly braces, even one line blocks...
    Sound advice. It was already on my list of "Forty-two Suggestions for Java Programmers' Peace Of Mind." (Also made the C list and the C++ list.) I always do this, and I recommend that others do it also.

    However, I do have a problem with the following:

    Quote Originally Posted by Fubarable View Post
    Doing this will solve your problem.
    Well, here's the thing:

    When you make an assertion like this and people follow the suggestion and find that it does not solve the problem, they might get discouraged.

    I wouldn't want that to happen, and I am pretty sure that you wouldn't either. I mean, did you actually test anything in the Original Poster's program related to your suggestion?

    Namely...
    Quote Originally Posted by Garron5899 View Post
    program keeps saying that it took 1 time to generate snake eyes
    Here's my assertion, borne out by my testing (aided by the use of print suggestions from my previous post):
    The fundamental problem reported by the Original Poster has nothing to do with whether one or two statements following the "else" are or are not enclosed in braces.


    Cheers!

    Z
    Last edited by Zaphod_b; July 20th, 2012 at 10:43 AM.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Java Programming Exercise

    I see exactly what you did. I wrote a poker game program and I did the exact same thing and I was stuck for a week figuring it out. It just shows how precise you have to be while typing code.


    What is wrong with this statement?

    while (snakeEyes = false);

    Figure that out and your problem should be fixed.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Java Programming Exercise

    That is a nasty error! that's why i always use
    while (snakeEyes);
    Or
    while (!snakeEyes);
    That way i will never make that mistake. But you should try to find out what is wrong with your code, so you know what the mistake is .

Similar Threads

  1. Need help with programming exercise.
    By X0BeachBabe0x in forum Loops & Control Statements
    Replies: 2
    Last Post: December 29th, 2011, 10:08 PM
  2. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 09:51 AM
  3. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 05:51 PM
  4. [SOLVED] Exercise for study course help?
    By SweetyStacey in forum Object Oriented Programming
    Replies: 12
    Last Post: April 25th, 2010, 03:01 PM
  5. JAVA exercise
    By gmilan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 02:30 PM