• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops  RSS feed

 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, so I recently started taking a java course at my Uni, but I can't quite seem to understand how loops are working. The goal i'm trying to achieve with my program is to hit the specied target by rolling a dice using Math.random. The part i'm struggling with is making the generator "roll" the dice automaticly until i hit my target, and not have me insert a new target after every failed attempt. If anyone is able to help me understand, it would be much appreciated!

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eirik, welcome to the Ranch!

Here's what you want your code to do:

1. Ask the user to enter the dice target and make sure it's a valid target.

2. Run the loop which repeatedly rolls the dice until the target is hit.

But your code doesn't do that; the asking-the-user part is inside the loop which is supposed to be the second part of the code.
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So by doing what you suggested it worked out fine, and that leads me to my seconds question. When I hit the desired number I want the message to also tell me how many attempts i needed to do so.



I want to replace the N with the number of required rolls, but i'm not sure how i'm gonna write the codeline that counts the number of times the program has gone through the loop.


 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a variable to count the number of times. Generally that would look like this:



Then after the loop is finished, you can use your counter variable, which says how many times the loop was executed.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I suggest you print your code in large writing on paper and go through the flow with a pencil. Look what happens if you get to line 7 because the user entered 7. Avoid System.exit; it won't do anything wrong there, but it can be dangerous in some other applications. Anyway, what will the user expect? They won't expect the program to stop before entering a new value. So you want something different for line 8. And when that part of the code is finished, do you really want else after it?
I don't like arithmetic with Math#random. I think it is error‑prone; you have got a formula which will work, but I prefer to use something different, as discussed here.
How have you been told to indent your code? Your indentation is very inconsistent; you shou‍ld decide on an indentation convention and stick to it rigidly. That way you will better be able to find errors. Look at lines 4 6 10 and 15. Line 10 is particularly risky because it is possible to look at it quickly and not realise where the else starts and ends. We have some formatting suggestions here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!