Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having trouble getting to while loop

 
Angela Li
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a part of the craps game, and I am having trouble getting from the else loop to the while loop. My method just seems to ignore the while loop but I need the while loop to work.
 
Atul Darne
Ranch Hand
Posts: 118
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code is little buggy..
you shouldn't use while loop that way.. try coding as simple..
 
Angela Li
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me a suggestion on what to use besides a while loop?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion is to take a step back and describe what you want this routine to do.

Your current code tests the parameter total for the values 7, 11, 2, 3, and 12. If total is not one of those values, you set point to total, firstRoll to false, and the return out of the routine; so unless total is one of those values, you are not going to reach your while loop.

You also have "while (<condition>) { return 0; }" -- a while loop is normally supposed to execute its body some number of times while the condition is true, but if your condition is ever true, the body of your loop returns out of the method, and that will end the loop.

I suggest that you describe in a "pseudoCode" what you want to do: use English, write a one-sentence description of the overall purpose of the routine, and then a step by step description at a low level of how it gets to that, without worrying about variables or java syntax. If you want to, you can put double slashes (//) in front of all of those so they can be comments in your routine.

Then you write your routine around them -- just leave the comments in place and write the statements to do the steps you want to do. They will help you keep "on track" for what you want to do, and will be excellent help for those of us willing to help you when you show it to us later.

rc
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you declare variable point?
Before while(), instead of if{} you can try switch(total){}.
This may help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic