Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to get two dice to roll a number  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

trying to get two dice to roll a number and keep printing the results until a double is met...

heres my code:



Now i know whats wrong...
the roll1 and roll2 are given a random number and then the loop starts and if its different it keeps printing...but the same result

please dont solve it for me...give me an idea of where i should be looking....or if i missed something else entirely

thanks ahead
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using while loops, there must be code inside of the loop that changes the values of the variables that are in the while () that control the looping or there must be a break statement that can exit the loop.
The values of roll1 and/or roll2 need to be changed inside of the loop.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi norm radder,

it seems like a bit of a catch 22

i cant put the roll1 and 2 variable in the loop cause then my while loop doesnt understand the comparison in my while loop,as they are not defined yet.
and if i try to declare them before the loop the same variable in my loop says it already exists...got to get my head around this.

thanks for the reply
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it i go it!!!

ok so i just initialized the variables...didnt need to assign a value
then put the random in the loop


thanks
 
Paul Clapham
Sheriff
Posts: 22530
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare them outside the loop, and change them inside the loop. If you want to ensure that a loop is executed at least once, then the "do... while()" loop format is what you need.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry...what was i on about...obviously i would have to assign a variable...or how else would it compare

here is my code,any criticism most welcome

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now lines 15 and 16 are always executed at least one time even if roll1 == roll2?  If you don't want that you need some extra logic.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:. . .  If you don't want that you need some extra logic.
Or a slightly different type of loop.

Avoid multiple print statements if possible; you will get slightly faster execution if you can change the two print instructions to one. Also avoid \n and \r. Use printf and %n instead. Or println instead of plain print.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pedant> a single one is called a "die".  The plural is "dice"</pedant>
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:please dont solve it for me...give me an idea of where i should be looking....or if i missed something else entirely

Well, you can't roll two dice without rolling one, so I think I'd start out by defining a Die class, maybe with roll() and value() methods.

Then "Dice" - any number of Dice - simply becomes a set (or even array) of Dies that you "roll" collectively.

The nice thing about that is that you can then create different types of Die, viz:

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!