user2962426 user2962426 - 7 months ago 7
Java Question

I have to make a program that takes a user input and multiples it and stores the product in a matrix

import java.util.Scanner;

public class Tests {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String inputString;

char flag = 'y';


while(flag != 'q' && flag != 'Q') {

System.out.print("Enter a positive integer:");

int value=input.nextInt ();



System.out.printf("Enter q to quit or any other key to quit: ");

input.nextLine();

inputString = input.nextLine();

flag = inputString.charAt(0);
}
}
}


This is the code i have so far. What the program needs to do is take the user input and multiply it and store the product in a matrix. For example if the user enters 3 the matrix needs to be 3x3 and have the product of 1 multiplied by 1, 1 multiplied by 2 and 1 multiplied by 3. Then do the same thing for 2 and 3.

Answer
 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    String inputString;
    char flag = 'y';
    int matrix[][];
    while (flag != 'q' && flag != 'Q') {
        System.out.print("Enter a positive integer:");
        int value = input.nextInt();
        System.out.printf("Enter q to quit or any other key to quit: ");
        if(value > 0){
            matrix = new int[value][value];
            displayOutput(matrix);
        }

        input.nextLine();
        inputString = input.nextLine();
        flag = inputString.charAt(0);
    }
}

private static void displayOutput(int[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {

        matrix[i][0] = i+1;
    }
    for (int j = 1; j < matrix.length; j++) {
        for (int i = 0; i < matrix.length; i++) {
            matrix[i][j] = matrix[i][0]*(j+1);
        }            
    }

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            System.out.print(" "+matrix[i][j]);
        }
        System.out.println("");
    }

}
Input: 3
Outputs: 1 2 3
         2 4 6
         3 6 9