在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 entered array is:"); for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } scanner.close(); } }
在这个例子中,用户被要求输入数组的大小和元素,并且输入的数组值最后被打印出来。