当前位置:首页 > Java

java如何从键盘输入数组

2026-02-05 04:20:15Java

从键盘输入数组的方法

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

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

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

分享给朋友:

相关文章

vue实现数组倒叙

vue实现数组倒叙

实现数组倒序的方法 在Vue中实现数组倒序可以通过多种方式完成,以下是几种常见的方法: 使用JavaScript的reverse()方法 // 在data中定义数组 data() { retu…

vue实现数组双向绑定

vue实现数组双向绑定

实现数组双向绑定的方法 在Vue中,实现数组的双向绑定通常需要结合v-model指令或自定义事件处理。以下是几种常见的方法: 使用v-model绑定数组 Vue的v-model指令默认支持表单元素的…

react如何给数组添加值

react如何给数组添加值

使用 push 方法(需注意不可变性) 在 React 中直接使用 push 会修改原数组,违反不可变原则。推荐先复制数组再操作: const [items, setItems] = useStat…

react 如何修改大数组

react 如何修改大数组

修改大数组的高效方法 在React中处理大数组时,直接修改原数组可能导致性能问题。以下是几种高效处理大数组的方法: 使用不可变更新 通过创建新数组而非直接修改原数组,可以避免不必要的渲染。例如使用扩…

react如何合并两个数组

react如何合并两个数组

合并两个数组的方法 在React中合并两个数组可以使用多种方法,以下是一些常见的方式: 使用扩展运算符(Spread Operator) const array1 = [1, 2, 3]; con…

react实现数组过滤

react实现数组过滤

数组过滤的基本方法 在React中实现数组过滤通常使用filter方法,结合状态管理(如useState)动态更新过滤结果。以下是一个基础示例: import React, { useState }…