0
votes

wanted to use a switch statement in Java, but the variable in the switch-statement (switch(variable)) should be compared to random numbers in the case-statements (case(randomNumber)). But I get an error message like that: "case expressions must be constant expressions". So does my case-statements doesn't work with random numbers? I thought that the error message means that i have to preface the variables with "final", but that didn't work either. So, are if ... else statements the only way? I hope I explained my problem well enough to understand. (I'm just beginning with java and the whole terminology that comes with coding)

       int number = 6; //some number between 0-9
   int randomNumber1 = ((int)(Math.random() * 10));
   int randomNumber2 = ((int)(Math.random() * 10));
   int randomNumber3 = ((int)(Math.random() * 10));
   
   switch(number)
   {
   case(randomNumber1):
       //some code here
   case(randomNumber2):
       //some code here    
   case(randomNumber3):
       //some code here
   default:
       //some code here
   }
3
I tested this by creating new variables and doing case(variable), but that didn't even work which is surprsing. - Madhu Sharma
Considering the targets of a switch statement must be constants, it's not surprising at all. - WJS

3 Answers

0
votes

The reason that it is not working is because you are doing

case(variable)

When I changed this to an actual number, it started working for me. It is saying constant expression required because it wants an actual number, not a number stored in a variable.

Hope this helps

0
votes

As explained, the (expression) in a case (expression): statement must be a constant.

As for if-else being the only way, another possibility (depending on what you're trying to do overall) is to use the random numbers as keys in a map, with values of objects with a method that can be executed to perform the action you want.

    Interface MyAction
    {
        public void doMyAction() {};
    }

    public class Eight implements MyAction
    {
        public void doMyAction() { System.out.println("I'm an eight"); }
    }

    public class Four implements MyAction
    {
        public void doMyAction() { System.out.println("I'm a four"); }
    }

    // ...
    HashMap<Integer, MyAction> actionMap = new HashMap<>();
    actionMap.put(8, new Eight());
    actionMap.put(4, new Four()); 

    // ...
    MyAction myAction = actionMap.get(randomNumber);
    if (myAction == null) { System.out.println("Don't have one of those"); }
        else { myAction.doMyAction(); }

It's hard to see what overall goal you're trying to accomplish, which may be why you're not getting more response -- people here often don't like proposing something that may turn out to be 'wrong', even for a situation that wasn't explained completely.

0
votes

As seen from the error you are getting, switch can only be used with constant expressions. To achieve what you want, you can only use branching with if, else if, and else.

if(number == randomNumber1){
    //some code here
} else if(number == randomNumber2){
    //some code here
} else if(number == randomNumber3){
    //some code here
} else {
    //some code here
}