import java.util.Scanner;
时间: 2024-05-08 12:16:51 浏览: 150
java代码-import java.util.Scanner;
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.
阅读全文