• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having trouble with switch, case,break,

 
Angela Li
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)

 
Angela Li
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code suggestion, I appreciate it.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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() ;
 
Angela Li
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aniket, that was a stupid of me.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure!!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic