In a Java program, enter different types of data from the keyboard

I have studied Java in college, but I have been using C/C++ all the time. Now I go back to Java and find that many of them are unable to survive. It really is Xiaobai who started from scratch.
Now, back to the topic, how to get the desired data from the keyboard in a Java program.
One of the most common and convenient ones is to import the Scanner in the package at the beginning of the program. The statement is as follows:
import java.util.Scanner;

In the program, a new Scanner object is required,
such as: Scanner sc=new Scanner(System.in);

If you want to read integer data, it is
int aa=sc.nextInt();

If you want to input float data, it is
float bb=sc.nextFloat();

If you want to input string data, it is
String cc=sc.nextLine();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324043840&siteId=291194637