How can you reuse a variable?

I’m trying to make some sort of non-graphic version of a game. It involves two dices and when you roll any number besides the ones listed, you roll again until you get the one on the first roll. What have I done wrong or is there another way to select another random number for d1 and d2.

void setup()
{
  int d1 = (int) random (6) + 1;
  int d2 = (int) random (6) + 1;
  int sum = 0;
  int point = d1 + d2;
  
  
  println (d1 + ", " + d2);
 
  switch (point)
  {
    case 2:
    case 3:
    case 12: println ("You lose.");
             break;
    case 7:
    case 11: println ("You win.");
             break;
    default: sum += point;
             int d1 = (int) random (6) + 1;
             int d2 = (int) random (6) + 1;
             int point = d1 + d2;
             println (d1 + ", " + d2);
             
             switch (point)
             {
               case 7: println ("You lose.");
                       break;
               default: while (sum != point)
                        {
                            int d1 = (int) random (6) + 1;
                            int d2 = (int) random (6) + 1;
                            int point = d1 + d2;
                            println (d1 + ", " + d2);
                        }
             }
  }
}
1 Like

Do you want to change d1 and d2 again? Also, please format your whole code.

Yes, and how do you format it? I’m still a beginner in Processing so I’m sorry if I don’t understand any terms.

To format it just highlight your code and press </> on the top of the editor. To change a variable just type <variable name> = <value>, so d1 = (int) random (6) + 1; without the int to set it.

1 Like

Thank you so much, I saw my mistake now. :smile: