Help with Switch-Case Statement
How do you get this in a switch-case statement or work with it?
if (age < 70){
JOptionPane.showMessageDialog(null,"People that are below the 70s are nothing special.");
}
elseif (age > 69 && age < 80){
JOptionPane.showMessageDialog(null,"People that are in their 70s are called septuagenarian.");
}
elseif (age > 79 && age < 90){
JOptionPane.showMessageDialog(null,"People that are in their 80s are called octogenarian.");
}
elseif (age > 89 && age < 100){
JOptionPane.showMessageDialog(null,"People that are in their 90s are called nonagenarian.");
}
else (age > 99 && age < 110){
JOptionPane.showMessageDialog(null,"People that are in their 100s are called centenarian.");
}
Thanks~
[1508 byte] By [
Beornza] at [2007-11-26 18:08:17]

>
> else (age > 99 && age < 110) {
> JOptionPane.showMessageDialog(null, "People that
> that are in their 100s are called centenarian.");
> }
>
C any mistakes ;-)
Switch-case cannot be use for checking ranges..
As per Java Specification, swtich case expects an integer and boolean cannot be used as param for switch.
In your case switch can be used like this.
int index = age /10;
switch(index) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
Your First case
break;
case 7:
your second case
break;
case 8:
third case
break;
case 9:
fourth case
break;
default:
fifth case
break;
Take note of the break statements. Its very important.But I wont prefer in this case. Code looks awkward and is not so meaningful.....