Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
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
 
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
 
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

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

Code:
lol, I got it, i just wasn't testing right. I wasted about an hour.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.