Results 1 to 10 of 10
  1. #1
    teelnaw is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default 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. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    What are you trying to do. Use a method to print the numbers or use a method to generate the random numbers?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    teelnaw is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    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 10:14 PM.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    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)
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    teelnaw is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks for the help.

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

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

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

    Java Code:
    int rows = getRandom();
    int columns = getRandom();
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    teelnaw is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    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. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    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.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    teelnaw is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    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. #10
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Haha, no problem man. Gratz.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 05:08 PM
  2. Replies: 1
    Last Post: 01-08-2010, 07:19 AM
  3. Replies: 14
    Last Post: 10-19-2009, 11:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Posting Permissions

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