The methods for obtaining input in Java are: use the Scanner class to read text input; use the BufferedReader class to read text input; use the Console class to read text input (only applicable to Java 9).
How to get the entered value in Java
In Java you can use System.in to get the user input of.
1. Use Scanner class
<code class="java">import java.util.Scanner; public class InputExample { public static void main(String[] args) { // 创建 Scanner 对象以从 System.in 读取输入 Scanner scanner = new Scanner(System.in); // 从控制台读取一行输入 String input = scanner.nextLine(); // 打印输入 System.out.println("您输入的是:" + input); } }</code>
2. Use BufferedReader class
<code class="java">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InputExample { public static void main(String[] args) throws IOException { // 创建 BufferedReader 对象以从 System.in 读取输入 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // 从控制台读取一行输入 String input = reader.readLine(); // 打印输入 System.out.println("您输入的是:" + input); } }</code>
3. Use Console class (Java 9 only)
<code class="java">import java.util.console.Console; public class InputExample { public static void main(String[] args) { // 创建 Console 对象以从 System.in 读取输入 Console console = System.console(); // 从控制台读取一行输入 String input = console.readLine(); // 打印输入 System.out.println("您输入的是:" + input); } }</code>
The above is the detailed content of How to get the input value in java. For more information, please follow other related articles on the PHP Chinese website!