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]);
}
}
}