PDA

View Full Version : Issue invoking a method with an array.




Hylekmi
Apr 10, 2012, 09:21 PM
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.

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



lee1210
Apr 10, 2012, 09:42 PM
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

Hylekmi
Apr 10, 2012, 09:45 PM
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

lee1210
Apr 10, 2012, 09:52 PM
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.

Hylekmi
Apr 10, 2012, 09:57 PM
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.

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