Results 1 to 7 of 7
Thread: Roulette game help
- 03-13-2011, 09:41 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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?
-
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?
-
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!"); } }
- 03-14-2011, 12:33 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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.
-
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.
-
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
making a program emulate roulette HELP!!
By manowar689 in forum New To JavaReplies: 13Last Post: 06-24-2010, 02:13 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
online ROULETTE CALCULATER
By dysfunktional in forum Java AppletsReplies: 2Last Post: 05-13-2009, 09:49 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks