Says missing curly brackets, but they're all there

Unless I’m missing something, this code should be correct. My goal was to make a circle (“Bubble”) which would be a random color. however, it says that I am missing curly brackets
“{” missing on line 4: int color = int(random(0, 7));
and “}” missing on line 38: }

Here is the full tab:

class BubRandCol
{
  int[] rgb = new int[3];
  int color = int(random(0, 7));
  
  if(color == 0)
  {
    int[] rgb = {255,0,0};
  }
  if(color == 1)
  {
    int[] rgb = {255,128,0};
  }
  if(color == 2)
  {
    int[] rgb = {255,255,0};
  }
  if(color == 3)
  {
    int[] rgb = {0,255,0};
  }
  if(color == 4)
  {
    int[] rgb = {0,0,255};
  }
  if(color == 5)
  {
    int[] rgb = {128,0,128};
  }
  if(color == 6)
  {
    int[] rgb = {0,0,0};
  }
  if(color == 7)
  {
    int[] rgb = {255,255,255};
  }
}

The only other tab I have says this:

void setup()
{
  size(700, 500);
}

You can’t have code like this in he middle of your class: you can just declare your variables here.

If you want to write some algorithm, you need to put them inside functions like so:

class BubRandCol
{
  int[] rgb = new int[3];
  int color = int(random(0, 7));
  
  void BubRandCol() {
    if(color == 0)
    {
      int[] rgb = {255,0,0};
    }
    if(color == 1)
    {
      int[] rgb = {255,128,0};
    }
    if(color == 2)
    {
      int[] rgb = {255,255,0};
    }
    if(color == 3)
    {
      int[] rgb = {0,255,0};
    }
    if(color == 4)
    {
      int[] rgb = {0,0,255};
    }
    if(color == 5)
    {
      int[] rgb = {128,0,128};
    }
    if(color == 6)
    {
      int[] rgb = {0,0,0};
    }
    if(color == 7)
    {
      int[] rgb = {255,255,255};
    }
  }
}

No more curly brackets problem :+1:

2 Likes