# Random Mult. Table

Printable View

• 10-29-2010, 08:36 PM
teelnaw
Random Mult. Table
I am trying to create a multiplication using random numbers by using a method. I have been able to create a random multiplication without passing it through at method. I can't find a decent piece on initializing and utilizing methods. I am throwing in the towel, my eyes are about to bleed and I have been working on the for the last 6 hours.

Here is what I have so far... appreciate any help..

public class array1
{
public static void main (String [] args)
{

int rand = (int) (Math.random() * 10);
rand = rand + 1;

int rand2 = (int) (Math.random() * 10);
rand2 = rand2 + 1;

int maxRows = rand;
int maxColumns = rand2;

for (int i = 1; i <= maxRows; i++) {

for (int j=1; j <= maxColumns; j++){
System.out.print (i * j + "\t");
}

System.out.print ("\n");

}
}
}
• 10-29-2010, 09:06 PM
joshdgreen
What are you trying to do. Use a method to print the numbers or use a method to generate the random numbers?
• 10-29-2010, 09:12 PM
teelnaw
I am trying to use a method to generate random numbers then use a loop to create the table and print the numbers...

This is ultimately what I need to do...

Generate 2 random numbers from 1 to 10.
Assign those random numbers to 2 variables - maxRow and maxColumn.
Create a method that receives the above 2 random numbers, and...
Produces a 2 dimensional multiplication table, as follows:
Write a for loop that will iterate through the rows from 1 to maxRow (the first parameter received)
Write a while loop that will iterate through the columns from 1 to maxColumn (the second parameter received)
• 10-29-2010, 09:16 PM
joshdgreen
Code:

```public static int getRandom() {     return (int) (Math.random() * 10); }```

Where "int" is the return type, and "getRandom" is the method name. Here's more information for creating methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
• 10-29-2010, 09:28 PM
teelnaw
thanks for the help.

How would this fit into the program. Would I embed the for loop within the method statement?
• 10-29-2010, 09:29 PM
joshdgreen
You would just call this method when you declare your random numbers:

Code:

```int rows = getRandom(); int columns = getRandom();```
• 10-29-2010, 11:34 PM
teelnaw
I still can't get this to work... no matter what I do it won't allow me to initialize this method.

package mutliTable;

public class mutiTable {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

public static int getRandom(){

return (int) (Math.random()*10);
}

int maxRows = getRandom();
int maxColumns = getRandom();

for (int i = 1; i <= maxRows; i++) {

for (int j=1; j <= maxColumns; j++){
System.out.print (i * j + "\t");
}

System.out.print ("\n");

}

}
}
• 10-29-2010, 11:40 PM
joshdgreen
You accidentally put your method INSIDE of the MAIN method. You must put your method outside of the main method but still INSIDE the class.
• 10-29-2010, 11:55 PM
teelnaw
I got it ... wooo hooo. Only took 15 hours... thank god work was slow :)

Thanks for you help... I have two books with a small forest each inside them that couldn't specify that. Thanks again.
• 10-30-2010, 12:17 AM
joshdgreen
Haha, no problem man. Gratz.