当前位置:首页 > Java

java如何从键盘输入数组

2026-02-05 04:20:15Java

从键盘输入数组的方法

在Java中,可以通过多种方式从键盘输入数组。以下是几种常见的方法:

使用Scanner类读取固定大小的数组

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        int[] array = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        System.out.println("Array elements are:");
        for (int num : array) {
            System.out.print(num + " ");
        }
        scanner.close();
    }
}

使用BufferedReader类读取数组

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the size of the array: ");
        int size = Integer.parseInt(reader.readLine());
        int[] array = new int[size];

        System.out.println("Enter the elements of the array separated by space:");
        String[] input = reader.readLine().split(" ");
        for (int i = 0; i < size; i++) {
            array[i] = Integer.parseInt(input[i]);
        }

        System.out.println("Array elements are:");
        for (int num : array) {
            System.out.print(num + " ");
        }
        reader.close();
    }
}

使用Scanner类读取字符串数组

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        String[] array = new String[size];
        scanner.nextLine(); // Consume the newline

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextLine();
        }

        System.out.println("Array elements are:");
        for (String str : array) {
            System.out.println(str);
        }
        scanner.close();
    }
}

使用命令行参数输入数组

public class Main {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments provided.");
            return;
        }

        System.out.println("Array elements are:");
        for (String arg : args) {
            System.out.print(arg + " ");
        }
    }
}

注意事项

  • 使用Scanner时,注意处理输入缓冲区中的换行符。
  • 使用BufferedReader时,需要处理IOException
  • 输入数据时,确保数据类型与数组类型匹配,避免NumberFormatException

以上方法可以根据具体需求选择适合的方式从键盘输入数组。

java如何从键盘输入数组

分享给朋友:

相关文章

jquery数组

jquery数组

jQuery 数组操作 jQuery 提供了多种方法来操作数组或类数组对象(如 jQuery 对象集合)。以下是常见的数组操作方法: 遍历数组 使用 $.each() 方法可以遍历数组或对象: v…

java如何定义数组

java如何定义数组

定义数组的基本语法 在Java中,数组可以通过以下两种方式定义: 方式一:声明数组并指定长度 数据类型[] 数组名 = new 数据类型[数组长度]; 例如: int[] numbers = n…

vue数组怎么实现

vue数组怎么实现

Vue 数组操作方法 在 Vue 中操作数组时,需要遵循响应式原则,确保视图能正确更新。以下是常见的数组操作方法: 使用变异方法 Vue 对数组的变异方法进行了封装,这些方法会触发视图更新: pu…

java如何遍历数组

java如何遍历数组

遍历数组的方法 Java中遍历数组有多种方式,以下是常见的几种方法: 使用for循环 通过索引逐个访问数组元素: int[] array = {1, 2, 3, 4, 5}; for (int…

java如何声明数组

java如何声明数组

声明数组的基本语法 在Java中,数组的声明需要指定数据类型和数组名称,并可通过以下两种方式完成: 数据类型[] 数组名(推荐) int[] numbers; String[] names…

vue实现数组排序

vue实现数组排序

vue实现数组排序的方法 在Vue中实现数组排序主要依赖JavaScript的数组排序方法,结合Vue的响应式特性确保视图同步更新。以下是几种常见实现方式: 使用JavaScript原生sort方法…