# How can you reuse a variable?

**URL:** https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842
**Category:** Coding Questions
**Created:** [November 23, 2018, 3:08am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842 "2018-11-23T03:08:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dennisreyes](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dennisreyes](https://discourse.processing.org/u/dennisreyes)
#### Post date: [November 23, 2018, 3:08am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842/1 "2018-11-23T03:08:08Z")

</div>

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.

```auto
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);
                        }
             }
  }
}

```

---

<div class="post-metadata">

### Author: ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)
#### Post date: [November 23, 2018, 3:11am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842/2 "2018-11-23T03:11:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dennisreyes](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dennisreyes](https://discourse.processing.org/u/dennisreyes)
#### Post date: [November 23, 2018, 3:12am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842/3 "2018-11-23T03:12:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)
#### Post date: [November 23, 2018, 3:16am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842/4 "2018-11-23T03:16:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dennisreyes](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dennisreyes](https://discourse.processing.org/u/dennisreyes)
#### Post date: [November 23, 2018, 3:19am UTC](https://discourse.processing.org/t/how-can-you-reuse-a-variable/5842/5 "2018-11-23T03:19:39Z")

</div>

Thank you so much, I saw my mistake now. 😄
