import java.util.Scanner;
时间: 2024-05-08 15:16:51 浏览: 148
As an AI language model, I cannot perform input/output operations or run Java code. However, here is an example of how the Scanner class can be used in Java to read user input from the console:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
```
In this example, we create a Scanner object to read input from the console using `System.in`. We then prompt the user to enter their name and read their input using the `nextLine()` method of the Scanner class. Finally, we print out a greeting using the user's name. Note that we close the Scanner object at the end to prevent resource leaks.
阅读全文