How Many Doubles?
The issue with my program is that it does not calculate how many times the specific value has occurred. I believe that I have everything in place, but I'm not sure to as why it wouldn't work. Any help and/or feedback is kindly appreciated. Thanks!
Ok, so basically this program is supposed to do the following:
Modify the die program so it rolls both dice 500 times. Count the number of times each value occurred.
Your output should look like this:
2's = n times
3's = n times
.
.
12's = n times
class HowManyDoubles{
public static void main (String[] args){
int die1, die2, times = 0, oneCount = 0, twoCount = 0, threeCount = 0, fourCount = 0, fiveCount = 0, sixCount = 0;
int sevenCount = 0, eightCount = 0, nineCount = 0;
int countAmount = 0, diceAmount = 0;
System.out.println ("Start rolling...");
while (countAmount != 500 && diceAmount != 500) {
die1 = (int)(Math.random()*6) + 1;
System.out.println ("First die: " + die1);
diceAmount++;
die2 = (int)(Math.random()*6) + 1;
System.out.println ("Second die: " + die2);
countAmount++;
}
for (int count = 0; count <= diceAmount || count <= countAmount; count++) {
switch (times) {
case 1:
oneCount += 1;
break;
case 2:
twoCount += 1;
break;
case 3:
threeCount += 1;
break;
case 4:
fourCount += 1;
break;
case 5:
fiveCount += 1;
break;
case 6:
sixCount += 1;
break;
case 7:
sevenCount += 1;
break;
case 8:
eightCount += 1;
break;
case 9:
nineCount += 1;
break;
}
}
System.out.println ("You rolled one's" + oneCount + " times.");
System.out.println ("You rolled two's" + twoCount + " times.");
System.out.println ("You rolled three's" + threeCount + " times.");
System.out.println ("You rolled four's" + fourCount + " times.");
System.out.println ("You rolled five's" + fiveCount + " times.");
System.out.println ("You rolled six's" + sixCount + " times.");
System.out.println ("You rolled seven's" + sevenCount + " times.");
System.out.println ("You rolled eight's" + eightCount + " times.");
System.out.println ("You rolled nine's" + nineCount + " times.");
}
}
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
I'm not sure to as why it wouldn't work
Are you getting errors? If so please post the full text of the messages.
Is the program output incorrect, if so please post the program's output, eplain what is wrong with it and show what the output should be.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Your code is very poorly formatted making it hard to read. Please edit your post and add indentations (3-4 spaces) to the code within {}s
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Are you getting errors? If so please post the full text of the messages.
Is the program output incorrect, if so please post the program's output, eplain what is wrong with it and show what the output should be.
The output is incorrect, as it's not identifying how many times the value has occurred, and no there aren't any errors.
This is how it's being outputted:
Output:
> run HowManyDoubles
Start rolling...
First die: 3
Second die: 4
First die: 6
Second die: 4
First die: 3
Second die: 5
First die: 3
Second die: 6
First die: 1
Second die: 2
First die: 4
Second die: 2
First die: 5
Second die: 1
First die: 4
Second die: 1
First die: 3
Second die: 2
First die: 4
Second die: 2
First die: 1
Second die: 6
First die: 2
Second die: 5
First die: 1
Second die: 6
First die: 1
Second die: 1
First die: 4
Second die: 1
First die: 2
Second die: 1
First die: 3
Second die: 1
First die: 4
Second die: 1
First die: 5
Second die: 1
First die: 4
Second die: 5
First die: 2
Second die: 6
First die: 2
Second die: 4
You rolled one's 0 times.
You rolled two's 0 times.
You rolled three's 0 times.
You rolled four's 0 times.
You rolled five's 0 times.
You rolled six's 0 times.
You rolled seven's 0 times.
You rolled eight's 0 times.
You rolled nine's 0 times.
>
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Your code is very poorly formatted making it hard to read. Please edit your post and add indentations (3-4 spaces) to the code within {}s
Sorry about that, but thanks for the feedback!
I can't seem to be able to edit my initial post, so I will post it again as a reply. Hope it's better!
class HowManyDoubles{
public static void main (String[] args){
int die1, die2, times = 0, oneCount = 0, twoCount = 0, threeCount = 0, fourCount = 0, fiveCount = 0, sixCount = 0;
int sevenCount = 0, eightCount = 0, nineCount = 0;
int countAmount = 0, diceAmount = 0;
System.out.println ("Start rolling...");
while (countAmount != 500 && diceAmount != 500) {
die1 = (int)(Math.random()*6) + 1;
System.out.println ("First die: " + die1);
diceAmount++;
die2 = (int)(Math.random()*6) + 1;
System.out.println ("Second die: " + die2);
countAmount++;
}
for (int count = 0; count <= diceAmount || count <= countAmount; count++) {
switch (times) {
case 1:
oneCount += 1;
break;
case 2:
twoCount += 1;
break;
case 3:
threeCount += 1;
break;
case 4:
fourCount += 1;
break;
case 5:
fiveCount += 1;
break;
case 6:
sixCount += 1;
break;
case 7:
sevenCount += 1;
break;
case 8:
eightCount += 1;
break;
case 9:
nineCount += 1;
break;
}
}
System.out.println ("You rolled one's " + oneCount + " times.");
System.out.println ("You rolled two's " + twoCount + " times.");
System.out.println ("You rolled three's " + threeCount + " times.");
System.out.println ("You rolled four's " + fourCount + " times.");
System.out.println ("You rolled five's " + fiveCount + " times.");
System.out.println ("You rolled six's " + sixCount + " times.");
System.out.println ("You rolled seven's " + sevenCount + " times.");
System.out.println ("You rolled eight's " + eightCount + " times.");
System.out.println ("You rolled nine's " + nineCount + " times.");
}
}
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
There was no need to post so many lines of print out. A few in the beginning and a few from the end. The rest should have been removed.
Can you describe the logic you are trying to implement in the code you have written?
I see more than one loop. What is each loop supposed to do?
Where do you change the value of the times variable used in the switch statement?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
There was no need to post so many lines of print out. A few in the beginning and a few from the end. The rest should have been removed.
Can you describe the logic you are trying to implement in the code you have written?
I see more than one loop. What is each loop supposed to do?
Where do you change the value of the times variable used in the switch statement?
Logically, the program is to roll the dice 500 times, which it does perfectly, and then it is supposed to count how many times each value has occurred. For example, how many times the number 1, 2, 3, 4, etc have been rolled.
The loop below, is used to make both dice roll 500 times before stopping.
while (countAmount != 500 && diceAmount != 500) {
This second loop, below, is supposed to keep count of each value occurred for up to 500 rolls. The initial count being 0, and as the count is accumulated with the "count++", it would keep going up to 500.
for (int count = 0; count <= diceAmount || count <= countAmount; count++) {
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Your case statement is based on the integer TIMES. This value never changes and is always zero. Try making a method that increments the ncounts and call that passing die1 and die2 and you should be ok.
I'm not sure what you exactly mean by that, as I have the counts for each number incrementing in each case. Could you help me further understand that, and thanks very much for your help!
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
the program is to roll the dice 500 times, which it does perfectly, and then it is supposed to count how many times each value has occurred.
How can it count the values if the counting is not done at the place where the numbers are available for counting? It should all be done in ONE loop.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
How can it count the values if the counting is not done at the place where the numbers are available for counting? It should all be done in ONE loop.
Well, as you know I am VERY new at this, and so I may not know everything, nor do this in the correct order, but I'm still trying. Thanks for your help so far though! The answer to that question would simply be, that the first loop counts the dice rolls (500), whereas the second one is left open and is for the switch. If there is a way to do this in only one loop, then I apologise for that, since I haven't yet learned how to do that, nor have I seen it.
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
How can it count the values if the counting is not done at the place where the numbers are available for counting? It should all be done in ONE loop.
If you don't mind, could you kindly show as to how I could fix my program?
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
What actually happens in the second loop?
Add a
case 0:
to the switch statement and add a println in the code for that case to see what the value of the times variable is when the code is executed.
Don't post the output.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
how I could fix my program?
Describe in pseudo code what you want the program to do.
Write some short statements in a list of what should happen.
Show how and when you get the numbers to be counted
and show when you count those numbers.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
What actually happens in the second loop?
Add a
case 0:
to the switch statement and add a println in the code for that case to see what the value of the times variable is when the code is executed.
Don't post the output.
The value of the times variable prints out as 0.
It also prints out that I rolled one's 501 times.
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Describe in pseudo code what you want the program to do.
Write some short statements in a list of what should happen.
Show how and when you get the numbers to be counted
and show when you count those numbers.
Haha, I'm sorry but I'm not sure of how to write my program in "pseudo code" format, but I can try to simply explain how this is supposed to be.
I already know that the rolling of the dice up to the amount of 500 is correct.
The next step in the program should be the counting of each variable on a die, for example the numbers to be counted are: 1, 2, 3, 4, 5, 6, 7, 8, 9.
At the end of the program, after the 500 rolls, it is to display how many times each variable occurred in the 500 rolls.
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
What actually happens in the second loop?
Add a
case 0:
to the switch statement and add a println in the code for that case to see what the value of the times variable is when the code is executed.
Don't post the output.
I understand that you're trying to say the issue resides in the switch variable TIMES remaining at 0, and I am not sure of how to overcome this issue. Also, about combining the loops into one loop, I am also not sure of how to do that.
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
What if you count the numbers in the same loop they are generated in?
It is permitted to do more than one thing in a loop:
begin the loop
Generate the numbers
Count the numbers
end the loop
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
What if you count the numbers in the same loop they are generated in?
It is permitted to do more than one thing in a loop:
begin the loop
Generate the numbers
Count the numbers
end the loop
Ok, I get what your trying to say, but again, I'm not sure as to how I would form one loop to also count the numbers. Do you mind showing me?
EDIT: As I try changing the variable TIMES to accumulate it, as you had stated that was one of the issues, my program crashes.
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0