到目前为止,我的代码就是这个:
import java.util.Scanner;
public class PG1 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
//declaration,creation, initialization
double [][] matrix = new double [i][j];
//print element in row i
for (i = 0; i < matrix.length;i++){
//print element j in row i
for (j = 0; j < matrix[i].length; j++) {
System.out.print("The matrix is: " + matrix[i][j]);
}
System.out.println();
}
}
}
所以基本上,我想根据用户的输入或矩阵的行和列打印0和1。非常感谢您的帮助。
输出:
输入矩阵的长度:4
矩阵:
0 1 1 1
0 0 0 0
0 1 0 0
1 1 1 1
第1行的全0
第3行的所有1
列上没有相同的数字
diagona上没有相同的数字
答案 0 :(得分:0)
public static void main(String [] args){
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
int count0s = 0;
int count1s = 0;
//declaration,creation,initialisation
double [][] matrix = new double [i][j];
//print element in row i
for ( i = 0; i < matrix.length;i++){
//print element j in row i
for ( j = 0; j < matrix[i].length; j++){
if(matrix[i][j] == 0){
count0s++
System.out.print("The matrix is: " + matrix[i][j]);
}
else if(matrix[i][j] == 1){
count1s++
System.out.print("The matrix is: " + matrix[i][j]);
}
}
System.out.println("Total no. of 0s="+count0s);
System.out.println("Total no. of 1s="+count1s);
}
}
}
答案 1 :(得分:0)
如果您不想打印0和1:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
int count0s = 0;
int count1s = 0;
//declaration,creation,initialisation
double [][] matrix = new double [i][j];
//print element in row i
for ( i = 0; i < matrix.length;i++){
//print element j in row i
for ( j = 0; j < matrix[i].length; j++){
if(matrix[i][j] == 0)
count0s++
else if(matrix[i][j] == 1)
count1s++
}
}
System.out.println("Total no. of 0s="+count0s);
System.out.println("Total no. of 1s="+count1s);
}
}