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]
# 1
What do you mean by work with it?
kajbja at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 2
I meant, I want this nest-if statement to be part of a switch-case statement.
Beornza at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 3
This doesn't look like something you want to convert to a switch statement.You could do it, but it would be awkward and quite stupid.
-Kayaman-a at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 4
> I meant, I want this nest-if statement to be part of> a switch-case statement.Why do you think that switch would be usable or good in this case?Kaj
kajbja at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 5

>

> 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..

AbiSSa at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 6
> How do you get this in a switch-case statement or> work with it?If ur so bent on using switch case then you need to check for each value that might occur. Thats the only way ;-)-TC
AbiSSa at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 7
You could switch on the value (age/10). You only have to check for the values 0, 1, 2 ... 10 (and 11 and 12 mayhap?)kind regards,Jos
JosAHa at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...
# 8

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.....

saracgia at 2007-7-9 5:39:55 > top of Java-index,Java Essentials,Java Programming...