当前位置:首页 > Java

java如何从键盘输入数组

2026-02-05 04:20:15Java

java如何从键盘输入数组

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中循环数组通常使用v-for指令,以下是几种常见场景的实现方式: 基本数组循环 <template> <div> <ul>…

vue实现数组监听

vue实现数组监听

监听数组变化的方法 Vue.js 提供了多种方式来监听数组的变化,主要包括以下几种方法: 使用 Vue 的响应式系统 Vue 通过重写数组的原型方法(如 push、pop、shift、unshif…

vue实现数组全选

vue实现数组全选

Vue 实现数组全选功能 在 Vue 中实现数组全选功能通常涉及以下几个步骤: 数据绑定与状态管理 创建一个数组来存储所有选项的数据,并添加一个 selectedItems 数组来存储已选中的项。使…

react如何倒着渲染数组

react如何倒着渲染数组

倒序渲染数组的几种方法 在React中倒序渲染数组可以通过多种方式实现,以下是几种常见的方法: 使用reverse()方法 直接调用数组的reverse()方法会改变原数组,因此在渲染前需要创建副本…

react如何定义一哥数组

react如何定义一哥数组

定义数组的方法 在React中,可以通过多种方式定义和使用数组。以下是常见的几种方法: 使用useState钩子定义状态数组 import { useState } from 'react';…

php数组实现队列

php数组实现队列

使用数组实现队列的基本方法 在PHP中,数组可以模拟队列的先进先出(FIFO)特性。通过内置的数组函数,可以轻松实现队列的基本操作。 初始化队列 $queue = []; 入队操作(enqueue…