Results 1 to 7 of 7
  1. #1
    linc186 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Roulette game help

    Hello, I'm writing a small roulette app, and I'm stuck here.
    try {
    int uinput = myScanner.nextInt();
    switch(uinput){
    case 1:
    money = 1000;
    break;
    case 2:
    money = 500;
    break;
    case 3:
    money = 100;
    break;
    }
    } catch (InputMismatchException e) {
    System.out.println("That isn't a whole number!");
    }
    That's how you choose a difficulty, you are prompted by type 1, 2, or 3 for different amounts of money, and that works perfectly. The try catch also works fine, so if I don't input an int, it prints "That isn't a whole number!" But, after that, I want to the user to be able to input the difficulty again, because if you don't type in the correct thing, the program sets money to zero and just continues. I want it so that if the user inputs something that isn't a whole number for it to print what I want it to print, an then have the user to be able to input it an infinite amount of times until it is correct. So, how would I say, while the input is not an int, keep having the user input a value?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to use a loop here, such as a while loop. Create a boolean variable above the while loop, say called loopAgain and set it = to true. Then make loopAgain the condition of the while loop. In the try block after the user has entered input (after the call to myScanner.nextInt()), set loopAgain = false. If an exception occurs, then the next line where you set loopAgain to false won't be reached but instead the catch block will be called.

    Clear as mudd?

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    So all you want it to do is allow the user to keep inputting until its a whole number?

    Java Code:
    boolean successful = false;
    while (!successful) {
      try {
        int userInput = myScanner.nextInt();
        switch(userInput) {};
        successful = true;
      } catch(InputMismatchException e) {
        System.out.println("That isn't a whole number!");
      }
    }

  4. #4
    linc186 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    fubarable, That doesn't work; if the user inputs something that isn't an int, it only infinitely loops giving the user no opportunity to input a value

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by linc186 View Post
    fubarable, That doesn't work; if the user inputs something that isn't an int, it only infinitely loops giving the user no opportunity to input a value
    It works fine for me and doesn't loop infinitely if done correctly. If it doesn't work for you, telling us "it doesn't work" doesn't help us help you. Instead post your code and we'll help correct it.

    besides ozzyman seems to have spoon fed you something like what I was suggesting.

    To ozzyman, it's usually better to let the OP's work through and create the code.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i would've thought you would ask for the user input inside the loop in the try curly-bracket with a jdialog or something to receive the input and if they press OK it would continue the script but if they press CANCEL it would break the loop.

    yeah thanks fubarable, i'm also new to java and figured i'd learn quicker by taking up these challenges.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ozzyman View Post
    yeah thanks fubarable, i'm also new to java and figured i'd learn quicker by taking up these challenges.
    That's fine, but perhaps posting pseudo-code would be better.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. making a program emulate roulette HELP!!
    By manowar689 in forum New To Java
    Replies: 13
    Last Post: 06-24-2010, 02:13 AM
  3. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  4. online ROULETTE CALCULATER
    By dysfunktional in forum Java Applets
    Replies: 2
    Last Post: 05-13-2009, 09:49 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •