How to input an array from the keyboard in Java
In Java, you can use the Scanner class to input an array from the keyboard. 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("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 input array is:");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
}
}
In the above code, first use the Scanner class to input the size of the array from the keyboard, then create an integer array of size. Next, use a loop to input the elements of the array from the keyboard and output the input array. Finally, close the Scanner object.