我有一个作业,它要求用户输入二维数组的大小,然后要求输入数组中的数字,然后进行一些计算(例如求和和最大值)。 我遇到的问题是,当它要求用户输入数组中的数字时,我无法让它按照分配的方式进行格式化。在以应有的格式显示最终数组时也遇到了麻烦。
用户输入示例:
输入[0][0]:
输入[0][1]:
等等
最终数组显示示例:
[1,2,3]
[4,5,6]
等等
这是到目前为止我的代码,我已经评论了我需要帮助的部分。
import java.util.Arrays;
import java.util.Scanner;
class Array2DMethods {
public static int[][] readInputs() {
Scanner keyboard = new Scanner(System.in);
System.out.print(" How many rows? ");
int rows = keyboard.nextInt();
System.out.print(" How many columns? ");
int columns = keyboard.nextInt();
int[][] ret = new int[rows][columns];
for (int i = 0; i < ret.length; i++) {
for (int j = 0; j < ret[i].length; j++) {
/*Need to format it like this: Enter [0][0]:
Enter [0][1]:
etc. until it forms the 2D Array but not sure how to get
it to print out like that.
*/
System.out.print(" Enter " + ret + ": ");
ret[i][j] = keyboard.nextInt();
}
}
return ret;
}
public static int max(int[][] anArray) {
int ret = Integer.MIN_VALUE;
for (int i = 0; i < anArray.length; i++) {
for (int j = 0; j < anArray[i].length; j++) {
if (anArray[i][j] > ret) {
ret = anArray[i][j];
}
}
}
return ret;
}
public static void rowSum(int[][] anArray) {
int rows = anArray.length;
int cols = anArray[0].length;
int[] sum = new int[rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i] += anArray[i][j];
}
}
for (int x = 0; x < anArray.length; x++) {
System.out.println("Sum of row " + x + " = " + sum[x]);
}
}
public static void columnSum(int[][] anArray) {
int rows = anArray.length;
int cols = anArray[0].length;
int[] sum = new int[cols];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
sum[i] += anArray[j][i];
}
}
for (int x = 0; x < anArray.length || x > anArray.length; x++) {
System.out.println("Sum of columnn " + x + " = " + sum[x]);
}
}
public static boolean isSquare(int[][] anArray) {
for (int i = 0, l = anArray.length; i < l; i++) {
if (anArray[i].length != l) {
return false;
}
}
return true;
}
public static void displayOutputs(int[][] anArray) {
System.out.println("Here is your 2Dim Array:");
for (int i = 0; i < anArray.length; i++) {
for (int j = 0; j < anArray[i].length; j++) {
System.out.print(anArray[i][j]);
/*When displaying the whole array before the end of the program
it needs to be formatted like this:
[1,2,3]
[4,5,6]
[7,8,9]
but I'm not sure how to get it to print like that.
*/
System.out.print(", ");
}
System.out.println();
}
}
}
class Arrays2DDemo {
public static void main(String[] args) {
System.out.println("Let's create a 2Dim Array!");
int[][] anArray = Array2DMethods.readInputs();
Array2DMethods.max(anArray);
System.out.println();
Array2DMethods.rowSum(anArray);
System.out.println();
Array2DMethods.columnSum(anArray);
Array2DMethods.isSquare(anArray);
System.out.println("\nIs this a square array? " + Array2DMethods.isSquare(anArray));
System.out.println();
Array2DMethods.displayOutputs(anArray);
}
}
感谢您给我的任何帮助。
最佳答案
对于输入部分:
System.out.print(String.format(" Enter [%d][%d]: ", i, j));
ret[i][j] = keyboard.nextInt();
对于输出部分:
for (int[] row : anArray) {
System.out.println(Arrays.toString(row));
}
关于java - Java中的二维数组打印格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281823/