| Author |
Having trouble with switch, case,break,
|
Angela Li
Greenhorn
Joined: Nov 04, 2011
Posts: 12
|
|
I'm suppose to write a class that asks for an int between one and five inclusive, then check if the int is really in the range, then use-- switch, case, break--to generate an appropriate message that corresponds with the number entered. And last return to the ask for int step.
This is what I have did so far but I am having trouble when I input an int inside the accepted range, it gives me the default.
|
 |
Timothy Oldbean
Greenhorn
Joined: Jun 23, 2011
Posts: 17
|
|
Hi Angela I'm new here too so I'm not a lot of help yet. You can make the code a lot more readable by using the "Code" button.
I'm pretty sure someone will come along with an answer to your question soon :-)
|
If it wasn't for physics and law enforcement, I'd be unstoppable!
|
 |
Angela Li
Greenhorn
Joined: Nov 04, 2011
Posts: 12
|
|
|
Thanks for the code suggestion, I appreciate it.
|
 |
Aniket S. Kulkarni
Ranch Hand
Joined: Jun 15, 2011
Posts: 86
|
|
@Angela Li: Why are you complicating the code?
The code is fine but...
Just change the return type of getChoice() method to int and replace the line no. 43 with
return keybd.nextInt() ;
|
Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
|
 |
Angela Li
Greenhorn
Joined: Nov 04, 2011
Posts: 12
|
|
|
Thanks Aniket, that was a stupid of me.
|
 |
Aniket S. Kulkarni
Ranch Hand
Joined: Jun 15, 2011
Posts: 86
|
|
My pleasure!!!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Why have you got the same line endNow = true; in six different places? That means your loop is invalid; you can only iterate it once.
|
 |
D. Ogranos
Ranch Hand
Joined: Feb 02, 2009
Posts: 213
|
|
You can also check for char constants in the switch statement. Your getChoice() method returns a char, so simply check for that:
Note the single quotes around the numbers.
|
 |
 |
|
|
subject: Having trouble with switch, case,break,
|
|
|