user2962426 - 2 years ago 57

Java Question

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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
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
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**