d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question With Switch Case Java > Error On Compilation
Add Reply New Topic New Poll
Member
Posts: 11,708
Joined: Jul 30 2007
Gold: 677.00
Oct 16 2013 11:51am
I tried many solution to solve this but i can't find how.

i tried to do case ( 2 <= ageMembre || ageMembre => 3 ) ::
dosn't work be cause i want the ageMembre to be equal to 2 or equal to 3

any idea how to fix it?


Quote
if ( ageMembre < 14 ) {        // Enfants
            switch ( ageMembre){
            case ( 2 < ageMembre > 3 )  :
                switch (groupeAlim){
                    case 1 : portionMin = 4;  portionMax = 4;
                    break ;
                    case 2 : portionMin = 3;  portionMax = 3;
                    break;
                    case 3 : portionMin = 2;  portionMax = 2; 
                    break;
                    case 4 : portionMin = 1;  portionMax = 1;
                    break;
                    default:
                    break;
                }
              case ( 5 < ageMembre > 8) :
                switch (groupeAlimen) {
                    case 1 : portionMin = 5;  portionMax = 5;
                    break ;
                    case 2 : portionMin = 4;  portionMax = 4; 
                    break ;
                    case 3 : portionMin = 2;  portionMax = 2; 
                    break ;
                    case 4 : portionMin = 1;  portionMax = 1; 
                    break ;
                }
              case ( 9 < ageMembre > 13) :
                switch (groupeAlimen) {
                    case 1 : portionMin = 6;  portionMax = 6;
                    break ;
                    case 2 : portionMin = 6;  portionMax = 6; 
                    break ;
                    case3 : portionMin = 3;  portionMax = 4;
                    break ;
                    case 4 : portionMin = 1;  portionMax = 2; 
                    break ;
                }// groupeAlim
            } // ageMembre


This post was edited by pute_Fg on Oct 16 2013 11:52am
Member
Posts: 2,355
Joined: Apr 17 2009
Gold: 361.00
Oct 16 2013 01:02pm
ageMembre => 3 basically is 2 <= ageMembre.

Anyways if you want to see if it's 2 or 3, why not use that as first cases?

case 1:
break;
case 2:
break;
case 3:
break;
default:
break;

Not that nice but something to work with.
Member
Posts: 11,708
Joined: Jul 30 2007
Gold: 677.00
Oct 16 2013 01:45pm
Quote (Onimo @ 16 Oct 2013 14:02)
ageMembre => 3 basically is 2 <= ageMembre.

Anyways if you want to see if it's 2 or 3, why not use that as first cases?

case 1:
break;
case 2:
break;
case 3:
break;
default:
break;

Not that nice but something to work with.



what i want is if the ageMembre = 2 then do the First switch. if ageMembre = 12 then do the Switch #3. but i don't know how to do it in a case parentheses
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 16 2013 01:46pm
What you are doing does not work in Java. Either rewrite that as an if-else if-else block or fix your case statements to not include the comparison operations.
Member
Posts: 11,708
Joined: Jul 30 2007
Gold: 677.00
Oct 16 2013 01:59pm
Quote (rockonkenshin @ 16 Oct 2013 14:46)
What you are doing does not work in Java. Either rewrite that as an if-else if-else block or fix your case statements to not include the comparison operations.



i changed the comparison operations for case ( 5 & 8 ) . Should it work?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 16 2013 02:08pm
Quote (pute_Fg @ Oct 16 2013 03:59pm)
i changed the comparison operations for case ( 5 & 8 ) . Should it work?


you can't use comparison operators in a switch. a comparison operation returns a boolean and switches don't work with booleans
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2013 04:25pm
Quote (labatymo @ Oct 16 2013 04:08pm)
you can't use comparison operators in a switch. a comparison operation returns a boolean and switches don't work with booleans


Quote (pute_Fg @ Oct 16 2013 03:59pm)
i changed the comparison operations for case ( 5 & 8 ) . Should it work?


Plus that's a bitwise operator, not a boolean operator.

The whole point of switch statements is to build a lookup table ahead of time. So it has to be constant.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll