import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Javamonothread1 {
// Var 10
public static void main(String[]args){
int n = 10;
int [][]first = new int [n][n];
int [][]second = new int [n][n];
int [][]result = new int [n][n];
//input
{
BufferedReader reader = new BufferedReader (new InputStreamReader (System. in));
System. out. println("input the size of matrix (more than 2 and less than 10)");
try {
n= Integer. parseInt (reader.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if ((n<2)|(n>10)){
n=10;
System. out. println("Invalid input! N set to 10");
}
System. out. println("Would you like to fill matrix manually(1) or randomly(0)?");
int answer = 0;
try {
answer= Integer. parseInt (reader.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (answer == 1){
for (int a=0;a<n;a++){
for (int b=0;b<n;b++){
System. out. println("Please,input the element FIRST"+a+b+":");
try {
first[a][b] = Integer. parseInt (reader.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
for (int a=0;a<n;a++){
for (int b=0;b<n;b++){
System. out. println("Please,input the element SECOND"+a+b+":");
|
|
try {
second[a][b] = Integer. parseInt (reader.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}}
else {
Random random = new Random();
for (int a=0;a<n;a++){
for (int b=0;b<n;b++){
first[a][b] = (random.nextInt(100)-random.nextInt(100));
second[a][b] = (random.nextInt(100)-random.nextInt(100));
}}}
System. out. println(" ");
System. out. println("Matrix 1 below");
System. out. println(" ");
for (int i=0; i<n;i++){
System. out. println(" ");
for (int j=0;j<n;j++){
System. out. print(first[i][j]+" ");
}}
System. out. println(" ");
System. out. println("Matrix 2 below");
System. out. println(" ");
for (int i=0; i<n;i++){
System. out. println(" ");
for (int j=0;j<n;j++){
System. out. print(second[i][j]+" ");
}}
}
//end of input
//main body
{
int sum = 0;
int dif = 0;
for (int a=0;a<n;a++){
sum = first[0][a]+second[a][0];
dif = second[a][0]-second[0][a];
for (int b=1;b<n;b++){
sum+=(first[b][a]+second[a][b]);
dif+=(second[a][b]-second[b][a]);
}
if (sum<dif){
for (int x=0;x<n;x++)
result[x][a]=first[a][x];
}
else {
for (int x=0;x<n;x++)
result[x][a]=(first[x][a]*second[a][x]);
}
}
}
//end of main body
//output
System. out. println(" ");
System. out. println("Resulting matrix below");
System. out. println(" ");
for (int i=0; i<n;i++){
System. out. println(" ");
for (int j=0;j<n;j++){
System. out. print(result[i][j]+" ");
}}
//end of output
}
}