共計 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 網 – 提供最優質的資源集合!
正文完