Java中數組的輸入方式有以下幾種:
Scanner
類來從鍵盤讀取輸入,并將輸入的值存儲到數組中。import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[5];
for (int i = 0; i < 5; i++) {
System.out.print("請輸入第" + (i + 1) + "個元素:");
arr[i] = scanner.nextInt();
}
scanner.close();
System.out.println("數組的內容為:");
for (int i = 0; i < 5; i++) {
System.out.println(arr[i]);
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = new int[args.length];
for (int i = 0; i < args.length; i++) {
arr[i] = Integer.parseInt(args[i]);
}
System.out.println("數組的內容為:");
for (int i = 0; i < args.length; i++) {
System.out.println(arr[i]);
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("數組的內容為:");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}