如何在控制台输入int类型的二维数组

在Java中,我们经常需要在控制台输入二维数组,而且数组的元素类型是int。本文将介绍如何在控制台输入int类型的二维数组,并给出代码示例和逻辑清晰的解释。

1. 使用Scanner类获取控制台输入

首先,我们需要使用Java提供的Scanner类来获取控制台输入。Scanner类提供了方便的方法来读取各种类型的输入,包括整数、浮点数、字符串等。

import java.util.Scanner;

Scanner scanner = new Scanner(System.in);
  • 1.
  • 2.
  • 3.

2. 输入二维数组的行数和列数

在输入二维数组之前,我们需要先输入数组的行数和列数,以便确定数组的大小。

System.out.println("请输入二维数组的行数:");
int rows = scanner.nextInt();

System.out.println("请输入二维数组的列数:");
int cols = scanner.nextInt();

int[][] array = new int[rows][cols];
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3. 输入二维数组的元素

接下来,我们可以通过循环逐个输入二维数组的元素。

System.out.println("请输入二维数组的元素:");
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        array[i][j] = scanner.nextInt();
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

4. 输出输入的二维数组

最后,我们可以输出输入的二维数组,以确认输入是否正确。

System.out.println("输入的二维数组为:");
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        System.out.print(array[i][j] + " ");
    }
    System.out.println();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

通过以上步骤,我们就可以在控制台输入int类型的二维数组了。下面是完整的代码示例:

import java.util.Scanner;

public class InputIntArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入二维数组的行数:");
        int rows = scanner.nextInt();

        System.out.println("请输入二维数组的列数:");
        int cols = scanner.nextInt();

        int[][] array = new int[rows][cols];

        System.out.println("请输入二维数组的元素:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                array[i][j] = scanner.nextInt();
            }
        }

        System.out.println("输入的二维数组为:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

结论

通过以上步骤和代码示例,我们学会了如何在控制台输入int类型的二维数组。这种方法可以方便地获取用户输入的数组,并进行后续的处理和操作。希望本文对你有所帮助!


附录

代码示例
import java.util.Scanner;

public class InputIntArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入二维数组的行数:");
        int rows = scanner.nextInt();

        System.out.println("请输入二维数组的列数:");
        int cols = scanner.nextInt();

        int[][] array = new int[rows][cols];

        System.out.println("请输入二维数组的元素:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                array[i][j] = scanner.nextInt();
            }
        }

        System.out.println("输入的二维数组为:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
表格
列1列2列3
123
456
甘特图
gantt
    title