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:
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.
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.
Re: Java Programming Exercise
Quote:
Originally Posted by
Garron5899
...
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.)
Code java:
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
Re: Java Programming Exercise
Quote:
Originally Posted by
Fubarable
..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
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
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
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?
Code java:
while (snakeEyes = false);
Figure that out and your problem should be fixed.
Re: Java Programming Exercise
That is a nasty error! that's why i always use Or 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 :).