Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

The while loop

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic