Results 1 to 8 of 8
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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,
    Java 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.

    Java 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)
    		{
    			
    		}
    	}
    }

  2. #2
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: Is it possible to store multiple numbers to one variable?

    For one, I suggest using java's Random class.
    Java Code:
    import.util.Random;
    then you use:
    Java 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:
    Java 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

  3. #3
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Is it possible to store multiple numbers to one variable?

    Quote Originally Posted by Xzar View Post
    For one, I suggest using java's Random class.
    Java Code:
    import.util.Random;
    then you use:
    Java 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:
    Java 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?
    Last edited by MrPosotive; 11-14-2012 at 07:50 AM.

  4. #4
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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
    Java 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;
    						}
    					}
    				}
    				
    				
    				}
    			}
    		}
    	}
    }

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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

  6. #6
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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...

  7. #7
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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
    Java 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.
    Last edited by MrPosotive; 11-15-2012 at 03:56 AM.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Is it possible to store multiple numbers to one variable?

    it is just having a temper tantrum
    Ha! I remember those days. There is always a reason, and always something wrong. You'll get better, and your code will too, just hang in there!

Similar Threads

  1. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 01:59 AM
  2. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 08:22 PM
  3. how to Store and recall numbers in java ?
    By Ak-Emm in forum New To Java
    Replies: 16
    Last Post: 09-28-2008, 12:49 PM
  4. how to store the previous of a variable
    By Randa in forum New To Java
    Replies: 7
    Last Post: 09-22-2008, 09:16 PM
  5. Replies: 4
    Last Post: 06-09-2008, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •