Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - In java, how to prompt the user to enter five integers from the keyboard and store them in the corresponding array elements?
In java, how to prompt the user to enter five integers from the keyboard and store them in the corresponding array elements?
For example, prompt the user to enter five numbers:12345; There is a space between each number here, so you can make a fuss from here;

Package test17;

Import java.util.scanner;

Class test 17 {

Public static void main(String[] args) {

System.out.println ("Please enter a number:");

Scanner input = new scanner (system. in);

string str = input . nextline();

string x[]= str . split(" "); Use split to divide by spaces and assign values to array x;

for(int I = 0; I<x. Length; i++)

system . out . print(x[I]+" ");

}

}

Another way is to assign values one by one by using the for loop, but the above method is relatively simple, and you can enter as many numbers as you like.

I hope it helps you!