• Post Reply Bookmark Topic Watch Topic
  • New Topic

The while loop  RSS feed

 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am not understanding the problem with the while loop. I understand the concept of the while loop and use it daily, and when I thought i understand the while loop she turns out that I only know her superficially, which sounds like my last few relationships. Now for my problem, I am creating a casino game called over and under. Have you heard of it? The gambler is allowed to make bets on what the dice will roll. Here is what the output looks like: Select your choice: O(ver), U(nder), S(even). How if the user by mistake presses 1 or d the computer prompts the same response Select your choice: O(ver), U(nder), S(even) > O. Now I thought this is a job for the while loop. I my rationale was it would check the userinput and when the condition matched we move forward with the program. I have used the while loop for numbers when comparing if a number is greater or lesser, so why not compare it as a match. This is what I thought of :

Here is some examples i tried all failed miserably. I was also thinking of using a switch statement, but I would also be using the while loop to check the condition.

Thanks for your help in advance.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's properly format and indent your code, so that it's much easier to see and understand its structure.

You have two while loops here. Why?

Look carefully at what happens here. In the first loop, you keep asking the question "Select your choice: O(ver), U(nder), S(even) >", until the user enters a number that's not 2 or 3, or until the user types in something that's not a number (if that happens, an InputMismatchException will be thrown by ms.nextInt).

In the second loop, you keep asking the question until what the user enters is different from what the variable 'o' contains (ignoring case). What 'o' contains is a line of text that the user entered before (in line 6).

That doesn't look at all like what you want your program to do.

Before writing any code, write down in normal human language the exact steps that you want to happen. If you've written down those steps, then translate them into Java code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!