久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java如何從鍵盤輸入一個數組

134次閱讀
沒有評論

共計 559 個字符,預計需要花費 2 分鐘才能閱讀完成。

在 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 input array is:");
        for (int i = 0; i < size; i++) {System.out.print(arr[i] + " ");
        }
        
        scanner.close();}
}

在上面的代碼中,首先使用 Scanner 類從鍵盤輸入數組的大小,然后創建一個大小為 size 的整型數組。接著通過循環從鍵盤輸入數組的元素,并輸出輸入的數組。最后關閉 Scanner 對象。

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2024-03-19發表,共計559字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 杭锦旗| 南皮县| 西藏| 吉首市| 金阳县| 唐河县| 蓬安县| 宜良县| 九龙城区| 鄄城县| 长丰县| 太湖县| 芮城县| 绥江县| 茌平县| 乾安县| 庆城县| 监利县| 白山市| 岳池县| 扶余县| 神池县| 汝南县| 子长县| 吉水县| 都江堰市| 临沂市| 台安县| 大冶市| 宜春市| 泸溪县| 郯城县| 兰溪市| 新建县| 兴国县| 青浦区| 舞钢市| 溧阳市| 山阳县| 嘉善县| 榕江县|