How to input values into an array in Java?
In Java, you can utilize the Scanner class to retrieve array values from user input. Here is an example code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数组的长度:");
int length = scanner.nextInt();
int[] arr = new int[length];
System.out.println("请输入数组的元素值:");
for (int i = 0; i < length; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("数组的元素值为:");
for (int i = 0; i < length; i++) {
System.out.println(arr[i]);
}
scanner.close();
}
}
After running the above code, the program will prompt for the length of the array, then prompt for the value of each element in the array. Finally, the program will output the values of the array.