• Post Reply Bookmark Topic Watch Topic
  • New Topic

allow user to roll dice again  RSS feed

 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a dice game. If the dice land on 7 the payer loses. 2, 3, 11, 12 the player wins. Any other number and the player gets to roll the dice again. The part I am stuck on is how to get the game to allow the player to roll the dice again if it doesn't land on one of the said numbers. I assume this involves changing the if statement I have used to a loop but I cant get it to work. What should I do? Sorry I know I am supposed to put that code in a box or something but I can't figure out how to do it.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Finch wrote:I assume this involves changing the if statement I have used to a loop but I cant get it to work. What should I do?

Show us what you tried and explain what problems you are having.

Sorry I know I am supposed to put that code in a box or something but I can't figure out how to do it.

See UseCodeTags
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to place all or some of the code from line 26 and downward into a loop. So that it will repeat itself if the player doesn't win or lose. I am still fairly new to working with loops and I can't get it too work. It either just prints out the outcome of the dice roll 10 times or doesn't work at all.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to think about what your loop body needs to contain and the conditions that are required for the loop to continue.

So write down what needs to happen each time the loop executes and what condition(s) need to be fulfilled for the loop to execute again.
Once you have written it out in English (or whatever your natural language is) you then, and only then, start thinking about converting that to code.

BTW Which of the loop constructs is the most appropriate for this type of problem?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Finch wrote:

You might also want to read up on conditional operators.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code looks like something which would fit nicely into a switch‑case statement. You cannot however fit your “point” into a switch‑case statement.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That code looks like something which would fit nicely into a switch‑case statement. You cannot however fit your “point” into a switch‑case statement.

I posted pre-caffeine. I agree that in this case a switch statement would be better than an if statement full of conditionals.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!