温馨提示×

java中如何表示输入数组的值

小亿
96
2024-02-20 09:42:30
栏目: 编程语言
Java开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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[] arr = new int[size];
        
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("The entered array is:");
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
        
        scanner.close();
    }
}

在这个例子中,用户被要求输入数组的大小和元素,并且输入的数组值最后被打印出来。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:JAVA中数组的长度怎么表示

0