-6

I have a problem with my code, I'm trying to make my script spawn a different every time I click the mouse, but I get an error with my switch because I use a random to choose a different number each time. I get this error every time I try to run the script and I can't find the error.

    Cannot switch on a value of type float. Only convertible int values, strings or enum variables are permitted

Some of the tags and names of variables is in danish as I'm danish.

int posX = 100;
int posX2 = 1820;
int dirX = 1;
int dirX2 = 1;
boolean drawforest = true;
int height = 1080;
int width = 1920;
//img = loadImage("BlueMorpho.png");
PImage img;
int storrelse = 15;
int storrelse2 = storrelse + 2;
float skift = random(0,2);

//setup()-metoden køres én gang, når sketchen startes
void drawBackground()
{
  color e = color(135, 206, 235);
  fill(e);
  rect(960, 270, 1920, 560);
}
void setup()
{
  size(1920, 1080); 
  rectMode(CENTER);
  drawBackground();
  color f = color(1, 142, 14);
  fill(f);
  rect(960, 820, 1920, 540);
  //noLoop();
}


//draw()-metoden kører i et loop, indtil den stoppes
void draw()
{
  switch(skift)
  {
  case 0:
    img = loadImage("BlueMorpho.png");
    break;
  case 1:
    img = loadImage("BlueMorpho2.png");
    break;
  default:
    img = loadImage("BlueMorpho3.png");
    break;
  }



  if (drawforest)
  {
    drawForest(); 
    drawforest = false;
  }
  drawBackground();
  drawCloud();
  drawCloud2();
  posX = posX+1;
  posX2 = posX2-1;
  if (posX>width-20 || posX<20)
  {
    dirX=-dirX;
  }
  if (posX2>width-20 || posX<20)
  {
    dirX2=-dirX2;
  }
}


void drawForest()
{
  for (int i = 0; i < 12; i++)
  {
    drawTree(random(55, 1865), 635 + 30*i);
  }
}

void drawTree(float xx, float yy)
{
  color c = color(139, 69, 19);
  fill(c);
  //noStroke();
  rect(xx, yy+30, 50, 100);
  color d = color(000, random(25, 100), 000);
  fill(d);
  //noStroke();
  ellipse(xx, yy-30, 125, 100);
  color g = color(100, 0, 0);
  fill(g);
  ellipse(random(xx-50, xx+50), random(yy-50, yy+18), 10, 10);
}

void drawCloud()
{
  color h = color(255, 255, 255);
  fill(h);
  ellipse(posX, 150, 100, 50);
  //posX += random(50,1920);
  noStroke();
}
void drawCloud2()
{
  color h = color(0, 0, 0);
  fill(h);
  ellipse(posX2, 200, 100, 50);
  //posX += random(50,1920);
}

void tegnSommerfugl(float xx, float yy)
{
  image(img, xx, yy, width/storrelse2, height/storrelse);
}

void mouseClicked()
{
  tegnSommerfugl(mouseX,mouseY);
}
Andreas2200
  • 47
  • 3
  • 6
  • You are trying to switch on the variable 'skift' which is of float type. Maybe change 'skift' to an int? – khriskooper Aug 15 '17 at 09:08
  • Base problem is declaration float skift, but hard to imagine what You want at design level. Maybe should be int, but not sure – Jacek Cz Aug 15 '17 at 09:08
  • 1
    Compiler is right, you cannot switch based on float, it needs to be either int, string or enum. – Pradeep Simha Aug 15 '17 at 09:09
  • If `float` is needed, try to use `if`/`else if` instead of `switch`/`case`... – Usagi Miyamoto Aug 15 '17 at 09:13
  • And beyond that, read about java coding style guides. Most people do not put opening braces on a new line. You **also** want to look into your naming ... having `color h` is extremely not helpful for your readers. What is "h" supposed to stand for? "huh"? "hah?" "hello"? Long story short: avoid using single character names (with the only exception of for loop index variables, and stuff like x, y). – GhostCat Aug 15 '17 at 09:14
  • I appreciate the comeback ;-) – GhostCat Sep 05 '17 at 10:22

2 Answers2

2

As the comments suggest: expected behavior. You can't switch on a variable of type float.

The answer is: that would be a bad idea anyway. Keep in mind that floating point numbers are "inaccurate" by design (see here for example). Whereas switch has the notion of exactly matching n different cases. But that is simply technically not possible for floating point numbers.

In that sense: step back, and change the type of that variable to int for example. It should be int skift - not float.

And as you then ask: "but how do I get a random int number?" - see here.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I did use it as an int at first but then it came up and said "Cannot convert from float to int" In the reference it says that random() needs a float value, if I'm not mistaken. – Andreas2200 Aug 15 '17 at 09:13
  • 1
    See my updates. And hint: try doing some research the next time. It is not like you are entering uncharted territory where no human set foot before. – GhostCat Aug 15 '17 at 09:16
0

The switch case statement works only with:

  • primitives: byte, short, char, int;

  • Classes-wrappers of the primitives: Character, Byte, Short, Integer

  • Enum и String (since JDK 7+)

    You can use a switch on floats with up to the digits of precision you're looking for:

  • multiply skift for the 10..0 to shift the point as you need

  • then use Math.round(skift) to get the integer

or use the other random method like GhostCat said

Community
  • 1
  • 1