"Case expressions must be constant expressions" with final int

I get an error saying “Case expressions must be constant expressions”. I couldn’t fix it no matter what. Is the only solution to change xSpeed as well as origxSpeed or is there a way to set origxSpeed to xSpeed without getting the error?
I won’t send the full code because its like 300+ lines long. Here is what I want to do:

float xSpeed=10
final int origxSpeed=int(xSpeed);
//or do I have to do final int origxSpeed=10?

  if ((key == '1') && game==true) { 
    switch(int(abs(xSpeed))) {
    case origxSpeed:
      xSpeed = xSpeed * 2/3; //for some reason " *= " has trouble with fractions
      ySpeed = ySpeed * 2/3; //for some reason " *= " has trouble with fractions
      break;
    case 2:
      xSpeed *= 1; // *= works here
      ySpeed *= 1;
      break;
    }
  }

Also, one more question. Why does " *= " have trouble with fractions such as 2/3 and how can I fix it? When I tried to use " *= " with a fraction the value didn’t change at all. It works perfectly fine with regular numbers and decimals. I need to get this done by next week so I need answers ASAP. Thanks!

1 Like

thanks for this information, Turbo


println( (2/3) );
println( (2/(float)3) );

this is called integer division problem


better here not use switch / case / break
it anyhow not makes the code shorter or more readable…
use a IF and print the THING , what makes the condition for the IF, first.

3 Likes

xSpeed = xSpeed * 2/3;xSpeed *= 2 / 3.0;
ySpeed = ySpeed * 2/3;ySpeed *= 2.0 / 3;

3 Likes

Thank you, it works. I didn’t know one of them had to be a float for it to work.

I actually have three cases. All I want to know is whether I can set origxSpeed to int(xSpeed) without that error.

In Java a constant expression is something that can be pre-calculated at compile-time. :mantelpiece_clock:

However, the value of origxSpeed can only be determined at run-time. :running_man:

If you depend on origxSpeed as 1 of the conditions, you should just use a vanilla if/else block. :brick:

2 Likes

Otherwise if I want to use switch then I have to change both variables right?

Of course, both can be a floating value too. :wink:

The rule is: At least 1 of the operands of the operator / needs to be a float or a double; otherwise the result is an int, w/ its fractional part removed. :face_with_monocle:

1 Like

Out of interest, is forum.processing.org connected with this website? Many of your posts were on that website when I was looking for answers earlier. If they aren’t, you must be having a hard time answering all the questions that come in here and there. Same goes for some other notable people like Chrisir, Quark etc.

Processing got a couple of retired forums.
This 1 here in Discourse is the current active 1.
And indeed, I link to my old posts from time to time.

1 Like

I see. Must be a having a hard time anyway haha. I won’t bother you any longer, thanks for your help!

Guys stupid question here. I need help with shortening this. bally -= (ballx-width)-imgSize; doesn’t work.

      bally = bally-(ballx-width)-imgSize;
1 Like

Here you are calculating ballx - width subtracting the the result and imgSize from bally THEN assigning the result to bally

Here you are calculating ballx - width then subtracting imgsize THEN subtracting the result from and assigning* (-=) to bally . You have a double negative

They are different operations

The correct statement is
bally -= ballx - width + imgSize

3 Likes