Is it possible to store multiple numbers to one variable?

• 11-14-2012, 05:43 AM
MrPosotive
Is it possible to store multiple numbers to one variable?
Hey guys im doing a project where i generate two random numbers, (supposed to be two die so the numbers will be between 1 & 6) if both numbers are even they will be added but die1's value will be tripled. If they are odd, they will be added but die2 will be tripled, if one is even the other is odd something will happen and if die 2 is even but die 1 is odd something else will happen. Now im not exactly sure how to compare the random numbers to all evens between 1 and 6 so i thought of a if statement like,
Code:

``` if (die1 == 2,4,6 && die2 == 1,3,5) { }```
but obviously that doesn't work, then i thought of storing 1, 3 , and 5 to a single variable but i cant figure out how to do that and i cant find out how online so i hope some one here can help me.

Code:

```public class Prog335h {         public static void main(String args[])         {                 int die1 = (int) (Math.random()*6);                 int die2 = (int) (Math.random()*6);                                 System.out.println("Die 1 rolled a " + die1);                 System.out.println("Die 2 rolled a " + die2);                                 if(die1 == 1  && die2 == 3)                 {                                         }         } }```
• 11-14-2012, 07:12 AM
Xzar
Re: Is it possible to store multiple numbers to one variable?
For one, I suggest using java's Random class.
Code:

`import.util.Random;`
then you use:
Code:

```int die1 = r.nextInt(6); int die2 = r.nextInt(6);```
as for comparing the numbers to even numbers and odd numbers, you will use a for loop and an array for each:
Code:

```int[] evens = {2,4,6}; int[] odds = {1,3,5}; for(int i = 0; i < evens.length; i++) { if(die1 == evens[i]) { //do what you need to do } } //same for odds```
• 11-14-2012, 07:39 AM
MrPosotive
Re: Is it possible to store multiple numbers to one variable?
Quote:

Originally Posted by Xzar
For one, I suggest using java's Random class.
Code:

`import.util.Random;`
then you use:
Code:

```int die1 = r.nextInt(6); int die2 = r.nextInt(6);```
as for comparing the numbers to even numbers and odd numbers, you will use a for loop and an array for each:
Code:

```int[] evens = {2,4,6}; int[] odds = {1,3,5}; for(int i = 0; i < evens.length; i++) { if(die1 == evens[i]) { //do what you need to do } } //same for odds```

Can you tell me what the [i] does next to evens and why i need the for loop?
• 11-14-2012, 08:05 AM
MrPosotive
Re: Is it possible to store multiple numbers to one variable?
So here is my current code, but ive run into a problem. It dosent seem to be initializing the loop because it will imidiatly say the app is terminated and not print out anythingin the testing box (Im using eclipse) and i have no idea why
Code:

```import java.util.Random; public class Prog335h {         public static void main(String args[])         {                 Random rand = new Random();                                 int die1 = rand.nextInt(6);                 int die2 = rand.nextInt(6);                                 int[] evens = {2,4,6};                 int[] odds = {1,3,5};                                 for(int i = 0; i < evens.length; i++)                 {                 if(die1 == evens[i] && die2 == odds[i])                 {                         System.out.println("Die 1 equals    " + die1);                         System.out.println("Die 2 equals    " + die2);                         System.out.println("value = " + (2*die1 + die2));                         break;                 }                 else                 {                         if(die1 == odds[i]  && die2 == evens[i])                         {                                 System.out.print("Die 1 equals    " + die1);                                 System.out.print("\nDie 2 equals    " + die2);                                 System.out.println("value = " + (die1 + 2*die2));                                 break;                         }                         else                         {                                 if(die1 == evens[i]  && die2 == odds[i])                                 {                                         System.out.print("Die 1 equals    " + die1);                                         System.out.print("\nDie 2 equals    " + die2);                                         System.out.println("value = " + (2*die1 + die2));                                         break;                                 }                                 else                                 {                                         if(die1 == evens[i]  && die2 == evens[i])                                         {                                                 System.out.print("Die 1 equals    " + die1);                                                 System.out.print("\nDie 2 equals    " + die2);                                                 System.out.println("value = " + (die1 + die2));                                                 break;                                         }                                         else                                         {                                                 if(die1 == odds[i]  && die2 == odds[i])                                                 {                                                         System.out.print("Die 1 equals    " + die1);                                                         System.out.print("\nDie 2 equals    " + die2);                                                         System.out.println("value = " + (die1 + die2));                                                         break;                                                 }                                         }                                 }                                                                                                 }                         }                 }         } }```
• 11-14-2012, 11:59 PM
Re: Is it possible to store multiple numbers to one variable?
Add some System.out.println's in there so see how far it gets. You can print variables out too - I bet you have a logic error in one of your if statements
• 11-15-2012, 03:36 AM
MrPosotive
Re: Is it possible to store multiple numbers to one variable?
Ok so i did like you said and put in two println's one above the variable decleration of die 1 and 2 and one righ tunder it above the evens and odds and strange thing, the whole thing worked...
• 11-15-2012, 03:38 AM
MrPosotive
Re: Is it possible to store multiple numbers to one variable?
Ok i figured it out, the dies are generating number 0 also, how can i get it to avoid 0?, never mind i figured it out i just made 2 if statements...ok never mind that didnt fix it, its stopping right after all. I think it might be the break; thats screwing it up cause it only works some times. Ok that didnt work heres the new code without breaks and my prinln's
Code:

```import java.util.Random; public class Prog335h {         public static void main(String args[])         {                 Random rand = new Random();                 System.out.println("Hi");//*************                 int die1 = rand.nextInt(6);                 int die2 = rand.nextInt(6);                 System.out.println(die1 + " " + die2);//*************                                 int[] evens = {2,4,6};                 int[] odds = {1,3,5};                 System.out.println("This is right after the decleration of the evens and odds");//*************                 if(die1 == 0)                 {                         die1++;                 }                 System.out.println("This is right after i add 1 to die1 if it = 0"); //***********************                 if(die2 == 0)                 {                         die2++;                 }                 System.out.println("This is right after i add 1 to die2 if it = 0");//*************                 for(int i = 0; i < evens.length; i++)                 {                 if(die1 == evens[i] && die2 == odds[i])                 {                         System.out.println("Die 1 equals    " + die1);                         System.out.println("Die 2 equals    " + die2);                         System.out.println("value = " + (2*die1 + die2));                                         }                 else                 {                         if(die1 == odds[i]  && die2 == evens[i])                         {                                 System.out.println("Die 1 equals    " + die1);                                 System.out.println("Die 2 equals    " + die2);                                 System.out.println("value = " + (die1 + 2*die2));                                                         }                         else                         {                                 if(die1 == evens[i]  && die2 == odds[i])                                 {                                         System.out.println("Die 1 equals    " + die1);                                         System.out.println("Die 2 equals    " + die2);                                         System.out.println("value = " + (2*die1 + die2));                                                                         }                                 else                                 {                                         if(die1 == evens[i]  && die2 == evens[i])                                         {                                                 System.out.println("Die 1 equals    " + die1);                                                 System.out.println("Die 2 equals    " + die2);                                                 System.out.println("value = " + (die1 + die2));                                                                                         }                                         else                                         {                                                 if(die1 == odds[i]  && die2 == odds[i])                                                 {                                                         System.out.println("Die 1 equals    " + die1);                                                         System.out.println("Die 2 equals    " + die2);                                                         System.out.println("value = " + (die1 + die2));                                                                                                         }                                         }                                 }                                                                                                 }                         }                 }         } }```
Ok so i added more lines in each if statement and i thought i figured out that something was wrong with the 3rd if statement or the 4th cause the two numbers were odd and it wouldn't go but i realized that the first or the second didn't show up in the window and i was confused, ran it again two odd numbers again and it worked... I give up this is really weird and im just going to turn it in cause nothing is wrong with it, it is just having a temper tantrum.

Thanks for all the help guys.
• 11-15-2012, 09:02 PM