wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes cant get counters to work correctly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant get counters to work correctly" Watch "cant get counters to work correctly" New topic
Author

cant get counters to work correctly

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
what i am trying to do is to get the program to simulate an 11 sided die rolling and then printing out the probability of each number getting rolled. something like this:

Number of Rolls:
10
Sum Of Dice Probability
1s   1.0
2s   0.0
3s   0.0
4s   0.0
5s   0.0
6s   0.0
7s   0.0
8s   0.0
9s   0.0
10s   0.0
11s   0.0

my problem is that i cant get the probability to correctly calculate. i have diagnosed it to the counter for each occurrence of trues.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think you should "play JVM" and pretend it's you executing your program: as written, what *exactly* is it going to do?

Then think about some of the following questions:
- What happens if you don't have curly braces surrounding everything you want to execute in a code block?
- How many times do you want to generate a random number?
- As written, how many rolls will be recorded for a number that is *never* rolled by the program?
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120


i updated to include this. did that fix what you meant by the random is never rolled? im not sure what to do with the "{}" because i dont know if i put what is in the loop in those, or if i put what is in the if statements.
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
You are missing most, if not all, of the braces for your loops. This affects the logic of your program.
Without braces, a control statement will have only a single statement associated with it. Perhaps it
would help to start with a simpler example.



Take the braces off from the outer for loop and see how it breaks the code. The braces can be removed
from the inner for loop without breaking the code because there is only one statement within the braces.
I always put the braces so that I don't forget them, or, in case I need to go back and add another statement
to the for loop, the braces are already there.

There's only two loops in this example, one outer and one inner. Go over the code until you understand how
the braces associate the blocks of code to the for statements. Make sure you understand this example with
only two loops before you try to write code with several nested loops.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
There is a much easier way to do it. Put your counters into an array, then use the "random" number to decide which member of the array to increment. You don't even have to start from 0; int[] elements are initialised to 0 as a default value.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
the only problem with arrays is that im in a class and we havent went over them, so i dont think we are supposed to use things we dont know about.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
do you guys think this is a better way to do the probability?


i got the print statements where it calculate from an example online, but i cant get them to work right.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

- What is the purpose of all those loops?
- If a given number (for example, 8) is never rolled, how many rolls will your code think it was rolled?
- Again--what is the purpose of curly brackets (the "{}" characters) in Java?

As reference point, the code to solve this problem is an order of magnitude shorter than you're currently making it. It's often helpful to think about the overall nature of the problem--here's one way I might think about it:

- I'm rolling a die n times, which sounds a lot like a for-loop.
- For each roll I record which number was rolled (you're using distinct variables, someone else suggested an array, which would eliminate quite a bit of code).
- After I've rolled the die an appropriate number of times I do the calculations (which you're doing correctly, more or less, although there seems to be a spurious semi-colon in your counter1 code).
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i know that i could make it very simple, but i am supposed to use nested loops. it is supposed to repeat for the number of times that the user enters.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That requirement baffles me, but the rest of my comments still stand.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Lecturers can set assignments with baffling requirements in the hope of teaching a particular topic.

But I can't see any nested loops. Lots of loops yes, but not nested. And, as you have been told before, your loops are incomplete because you have not put enough {} is.
 
Consider Paul's rocket mass heater.
 
subject: cant get counters to work correctly
 
Similar Threads
dice game
Dice probability program, using nested loops.
Help.
Java Help: NullPointerException
Nested Loop Control