溫馨提示×
Java中數(shù)組的輸入方式有以下幾種:
Scanner
類來從鍵盤讀取輸入,并將輸入的值存儲(chǔ)到數(shù)組中。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) + "個(gè)元素:");
arr[i] = scanner.nextInt();
}
scanner.close();
System.out.println("數(shù)組的內(nèi)容為:");
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("數(shù)組的內(nèi)容為:");
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("數(shù)組的內(nèi)容為:");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}