1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Issue invoking a method with an array.

Discussion in 'Mac Programming' started by Hylekmi, Apr 10, 2012.

  1. macrumors regular

    #1
    I have an array created in a separate method that returns the array, and i want it to pass to another method that will "test" it. Im having issues getting the New method to accept the array along with how to invoke it.
    Code:
    public class Problem2 
    {
    	public static void main (String[]args  )
    	{
    		welcome();
    		rollDie();
    		
    		
    	}//end main
    	
    	public static void welcome()
    	{
    	 System.out.println("Welcome to My game!");
    	}//end welcome
    	
    	//Rolls a die 100 times then stores the result in an array.
    	public static int[] rollDie()
    	{
    	
    		int[] dieRoll = new int[100];
    		
    		 for(int i=0;i<100;i++)
    		 {
    		 dieRoll[i]=(int) (Math.random()*6)+1;
    
    		 }//end for
    		 return dieRoll;
    		
    		 
    		 
    	}//end roll die
    	
    	public static void outputResults(int dieRoll[])
    	{
    	System.out.println(dieRoll);
    	}
    }//end problem2
    
     
  2. macrumors 68040

    lee1210

    #2
    So main takes an array as an argument. Where are the [] in relation to the type and argument name? How does this compare to your method? Also, where are you calling outputResults? Also, (random()*6)+1, what the heck kind of die are you rolling? It has Integer.MAX_VALUE sides with 1,7,13,19,etc. Once random returns a number > (Integer.MAX_VALUE - 1) / 6 you'll overflow your ints. I think you're looking for %.

    -Lee
     
  3. macrumors regular

    #3
    The question is how to call outputresults. The (random()*6)+1 is code that my instructor gave me. I don't know how to make OutputResults accept dieRoll
     
  4. macrumors 68040

    lee1210

    #4
    You have fix the syntax of ouputResults, then:
    outputResults(diceRoll());

    You also need to fix the math of diceRoll if you haven't already.

    -Lee

    Edit: oops. Math.random() will return a double between 0 and 1. I'm used to seeing a random int from 0 to maximum int value, then mod 6. Your current implementation could yield 7, though.
     
  5. macrumors regular

    #5
    Code:
    lol, I got it, i just wasn't testing right. I wasted about an hour.
     

Share This Page