该代码似乎在运行,除了我得到的不是指定的(由用户)大小的矩阵,而是我认为的是堆地址这是当用户输入2作为大小然后输入4个数字时返回的内容:
输入矩阵大小:2逐行输入2 x 2矩阵:2 3 4 5行排序矩阵是… [[D @ 3c954549BUILD SUCCESSFUL(总时间:8秒)
这是代码…。谢谢您。
import java.util.Scanner; public class Exercise7_26M { public static void main (String[]args) { //Prompt user for input of matrix size System.out.println("Enter matrix size: "); Scanner input = new Scanner(System.in); int size = input.nextInt(); double[][] m = new double [size][size]; //prompt user for input of array System.out.print("Enter a " + size + " by " + size + " matrix row by row: "); for (int row = 0; row < 2; row++) for (int column = 0; column < 2; column++) m[row][column] = input.nextDouble(); System.out.print("The row-sort matrix is..." + m); }
Java数组不会覆盖,toString()因此您将从中获取默认实现Object。相反,您可以使用Arrays.deepToString(Object[])like
toString()
Object
Arrays.deepToString(Object[])
System.out.println("The row-sort matrix is..." + Arrays.deepToString(m));