Java 简明教程

Java - User Input

User Input in Java

要在 Java 中从用户获取输入,可以使用 Scanner 类。 Scanner 类是 java.util package 的内置类。

To take input from the user in Java, the Scanner class is used. The Scanner class a built-in class of java.util package.

Java Scanner class 提供许多内置方法从用户获取不同类型的用户输入。

Java Scanner class provides many built-in methods to take different types of user inputs from the users.

How to Use Scanner Class to Take User Input?

以下步骤介绍了如何在 Java 中将 Scanner 类用于用户输入:

The following are the steps to use Scanner class for the user input in Java −

Step 1: Import Scanner Class

首先,您需要导入 Scanner 类以使用其方法。要导入 Scanner 类,请使用以下导入语句:

Fist you need to import the Scanner class to use its methods. To import the Scanner class, use the following import statement −

import java.util.Scanner

Step 2: Create Scanner Class’s Object

导入 Scanner 类后,您需要创建其对象以使用其方法。若要创建 Scanner 类的对象,请调用 Scanner() 构造函数。

After importing the Scanner class, you need to create its object to use its methods. To create an object of the Scanner class, invoke Scanner() constructor.

以下是创建 Scanner 类的对象语句:

Below is the statement to create an object of Scanner class −

Scanner obj = new Scanner(System.in);

Step 3: Take User Input

Scanner 类提供了各种有用的方法,用来获取不同类型用户输入。例如,如果要输入整数,请使用 nextInt() 方法。

Scanner class provides a variety of methods which are useful to take user input of different types. For example, if you want to input an integer value, use nextInt() method.

以下是获取 Java 中用户输入的语句:

The following is the statement to take user input in Java −

int age = obj.nextInt();

上述语句将等待来自用户的整数输入。当用户提供一个整数时,它将分配给“age”变量。

The above statement will wait for an integer input from the user. When user provides an integer value, that will be assign to "age" variable.

Example of User Input in Java

在以下示例中,我们从用户读取两个整数,进行计算并打印它们的和:

In the following example, we are reading two integers from the user, calculating, and printing their addition −

// Importing the class
import java.util.Scanner;

public class AddTwoNumbers {
   public static void main(String[] args) {
      // Creating an object of Scanner class
      Scanner sc = new Scanner(System.in);

      // Reading two Integer numbers
      // using nextInt() method
      System.out.print("Enter the first number: ");
      int num1 = sc.nextInt();

      System.out.print("Enter the second number: ");
      int num2 = sc.nextInt();

      // Calculating the sum
      int sum = num1 + num2;

      // Printing the su
      System.out.println("The sum of the two numbers is: " + sum);
   }
}
Enter the first number: 10
Enter the second number: 20
The sum of the two numbers is: 30

Methods for Different Types of User Inputs

Scanner 类为不同用户输入提供了不同方法。要探索用于不同用户输入的所有方法,请查看下表:

The Scanner class provides different methods for the different types of User inputs. To explore all methods for different user inputs, look at the table below −

Integer Input from the User

nextInt() method 用于从用户获取整数输入。

The nextInt() method is used to take input of an integer from the user.

在以下示例中,我们以整数形式进行输入:

In the following example, we are taking an integer as an input −

// Importing the class
import java.util.Scanner;

public class IntegerInput {
   public static void main(String[] args) {
      // Creating an object of Scanner class
      Scanner sc = new Scanner(System.in);

      // Reading an Integer Input
      System.out.print("Input an integer value: ");
      int int_num = sc.nextInt();

      System.out.print("The input is : " + int_num);
   }
}
Input an integer value: 101
The input is : 101

Float Input from the User

nextFloat() method 用于从用户获取浮点值输入。

The nextFloat() method is used to take input of a float value from the user.

在以下示例中,我们以浮点形式进行输入:

In the following example, we are taking a float as an input −

// Importing the class
import java.util.Scanner;

public class IntegerInput {
   public static void main(String[] args) {
      // Creating an object of Scanner class
      Scanner sc = new Scanner(System.in);

      // Reading a Float Input
      System.out.print("Input a float value: ");
      float float_num = sc.nextFloat();

      System.out.print("The input is : " + float_num);
   }
}
Input a float value: 12.345
The input is : 12.345

String Input from the User

nextLine() method 用于从用户获取浮点值输入。

The nextLine() method is used to take input of a float value from the user.

在如下示例中,我们将一个字符串作为输入 −

In the following example, we are taking a string as an input −

// Importing the class
import java.util.Scanner;

public class IntegerInput {
   public static void main(String[] args) {
      // Creating an object of Scanner class
      Scanner sc = new Scanner(System.in);

      // Reading a String Input
      System.out.print("Input a string value: ");
      String str = sc.nextLine();

      System.out.print("The input is : " + str);
   }
}
Input a string value: Hello World
The input is : Hello World