Joseph N
Joseph N

Reputation: 13

User based Multiplication Table

Hi I need help with my code. Im trying to make a user base input multiplication table using a Scanner and a method, but i dont know how to get the user base input from the main method to the method i created, here is what i have so far:

public static void multiplicationTable(int i){
for (int i=1;i<=size;i++){
for (int j=1;j<=size;j++)
System.out.print("\t"+i*j);
System.out.println(); }
}
public static void main (String[] args) {
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt ();
}

Upvotes: 1

Views: 565

Answers (2)

Rahman
Rahman

Reputation: 3785

You can pass like :

    System.out.println("This program displays a multiplication table.");
    Scanner size = new Scanner(System.in);
    System.out.println("Enter a positive integer: ");
    int n = size.nextInt ();
    multiplicationTable(n); \\ pass here

Upvotes: 1

Tsung-Ting Kuo
Tsung-Ting Kuo

Reputation: 1170

Actually your codes are pretty closed. Please see below (I have tested the codes):

public static void multiplicationTable(int size) {
    for (int i = 1; i <= size; i++) {
        for (int j = 1; j <= size; j++) {
            System.out.print("\t" + i * j);
        }
        System.out.println();
    }
}

public static void main(String[] args) throws Exception {
    System.out.println("This program displays a multiplication table.");
    Scanner size = new Scanner(System.in);
    System.out.println("Enter a positive integer: ");
    int n = size.nextInt();
    multiplicationTable(n);
}

Upvotes: 0

Related Questions