# Thread: Random Mult. Table

1. Member
Join Date
Oct 2010
Posts
5
Rep Power
0

## 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");

}
}
}

2. What are you trying to do. Use a method to print the numbers or use a method to generate the random numbers?

3. Member
Join Date
Oct 2010
Posts
5
Rep Power
0
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)
Last edited by teelnaw; 10-29-2010 at 09:14 PM.

4. Java 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)

5. Member
Join Date
Oct 2010
Posts
5
Rep Power
0
thanks for the help.

How would this fit into the program. Would I embed the for loop within the method statement?

6. You would just call this method when you declare your random numbers:

Java Code:
```int rows = getRandom();
int columns = getRandom();```

7. Member
Join Date
Oct 2010
Posts
5
Rep Power
0
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");

}

}
}

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

9. Member
Join Date
Oct 2010
Posts
5
Rep Power
0
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. Haha, no problem man. Gratz.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•