Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default [SOLVED] Method trouble >:0!

    Variable random1 and random2 might not have been initialized this is the error im getting. The error is the highlighted code.

    Any suggestion on what i need to change.


    Java Code:
    public class Gamecrap
    {
    	public static void main(String[]args)
    	{
    		int random1;
    		int random2;
    
    		[COLOR="SeaGreen"]int k = result(random1,random2);[/COLOR]
    
    //you lose
    	if (k==2 || k==3 || k==12)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"="+k+"\nYou lose");
    		}
    //you win
    	 if (k==7 || k==11)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nYou win");
    		}
    //continue
    	if (k==4 || k==5 || k==6 || k==8 || k==9 || k==10)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nPoint is "+k);
    		int j = result(random1,random2);
    
    		while (j != k || j != 7){
    		 j =  result(random1,random2);
    		}
    		if (j == k)
    		System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou win");
    		else if(j == 7)
    		System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou lose");
    		}
    }
    		public static int result(int random1, int random2)
    		{
    			random1 = (int)(Math.random()*6)+1;
    			random2 = (int)(Math.random()*6)+1;
    		 	int total = random1 + random2;
    	   	 	return total;
    }
    
    
    }
    Last edited by PureAwesomeness; 02-20-2009 at 04:45 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    initialize variables random1 and random2 before the highlighted line

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    initialize variables random1 and random2 before the highlighted line
    i did!

    int random1;
    int random2;
    its above the highlighted code

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    initialize means this:
    int random1 = 0;
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    but then the all the "system.out.println" stuff will have 0 as random1 and random2.
    i want the random1 and random2 get the number from the method.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default oh boy...

    To initalize a variable means to assign a value to it. If you don't give the variables values, how can you use them? For example:
    Java Code:
    c = a + b;
    Now, please tell me the value of variable "c". Come on... I'm waiting...
    See? you can't. Neither can a program.

    Now if I give you the following:
    Java Code:
    a=5;
    b=4;
    c= a + b;
    Now, please tell me the value of "c". ah-ha... now you can tell me the value of "c".

    Your result() method could use some working on, but I'm too tired right now...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    so i need to create a method for random1 and random2?

  8. #8
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Try this:

    public class Gamecrap
    {
    public static void main(String[]args)
    {
    int random1 = random();
    int random2 = random();

    int k = result(random1,random2);

    //you lose
    if (k==2 || k==3 || k==12)
    {
    System.out.println("You rolled "+random1+"+"+random2+"="+k+"\nYou lose");
    }
    //you win
    if (k==7 || k==11)
    {
    System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nYou win");
    }
    //continue
    if (k==4 || k==5 || k==6 || k==8 || k==9 || k==10)
    {
    System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nPoint is "+k);
    int j = result(random1,random2);

    while (j != k || j != 7){
    j = result(random1,random2);
    }
    if (j == k)
    System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou win");
    else if(j == 7)
    System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou lose");
    }
    }

    public static int result(int random1, int random2)
    {
    int total = random1 + random2;
    return total;
    }


    public static int random() {
    int number = (int)(Math.random()*6)+1;
    return number;

    }


    }
    Basically you're initialising random1 & 2 and assigning them integers - then your using result to actually give u a result based on those numbers.

  9. #9
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    so i need to create a method for random1 and random2?
    Essentially as per above - Yes! and modify your result method.

  10. #10
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    oK thank you now i got that part.
    there is another problem that i need help on
    is that the highlighted part. if the k satisfy that condition it does go all the way through.
    i want that code to continue to get the random number until it satisfy the condition of either equals to k or 7

    Java Code:
    public class Gamecrap
    {
    	public static void main(String[]args)
    	{
    		int random1= dice();
    		int random2= dice();
    
    		int k = result(random1,random2);
    
    //you lose
    	if (k==2 || k==3 || k==12)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"="+k+"\nYou lose");
    		}
    //you win
    	 if (k==7 || k==11)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nYou win");
    		}
    [COLOR="SeaGreen"]//continue
    	if (k==4 || k==5 || k==6 || k==8 || k==9 || k==10)
    		{
    		System.out.println("You rolled "+random1+"+"+random2+"= "+k+"\nPoint is "+k);
    
    		random1= dice();
    		random2= dice();
    		int j = result(random1,random2);
    
    		while (j != k || j != 7){
    		random1= dice();
    		random2= dice();
    		 j =  result(random1,random2);
    		}
    
    		if (j == k)
    		System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou win");
    
    		else if(j == 7)
    		System.out.println("You rolled "+random1+"+"+random2+"= "+j+"\nYou lose");
    		}[/COLOR]
    }
    		public static int dice() {
    		int number = (int)(Math.random()*6)+1;
    		return number;
    
    }
    		public static int result(int random1, int random2)
    		{
    		 	int total = random1 + random2;
    	   	 	return total;
    }
    }

  11. #11
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    You would then have your own 'method' for the dice rolls (random1, random2) and create a while loop.
    make a boolean (dont need to but makes it nice) set it to false.
    then do while booleanA is not equal to 'true' run this code.
    then you can have your two if statements and if those are met then set the boolean to 'true' to break the loop, another thing you can do is add a counter on it which adds 1 each loop so you know how many rolls it took to complete!

Similar Threads

  1. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 PM
  2. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 05:02 PM
  3. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •