Number input reading skills in Java programming
In Java programming, operations that often involve reading numbers from data input by the user are often involved. Reading and processing numeric input correctly is important for writing well-functioning, easy-to-use programs. This article will introduce you to some techniques for reading numeric input in Java, along with specific code examples.
The Scanner class in Java provides convenient methods to read console input. The Scanner class makes it easy to read various types of data, including integers, floating point numbers, and other basic data types. The following is a simple sample code that demonstrates how to use the Scanner class to read integers entered by the user:
import java.util.Scanner; public class NumberInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个整数:"); int num = scanner.nextInt(); System.out.println("您输入的整数是:" + num); scanner.close(); } }
In the above example, a Scanner object is first created, and then the nextInt() method is called to read the user input. integer. Finally, the integer entered by the user is output.
When the data entered by the user is not a valid number, an input exception will occur. In order to ensure that the program can run normally, we can use the try-catch statement to handle exceptions. The following is a sample code that demonstrates how to handle non-numeric user input:
import java.util.InputMismatchException; import java.util.Scanner; public class NumberInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print("请输入一个整数:"); int num = scanner.nextInt(); System.out.println("您输入的整数是:" + num); } catch (InputMismatchException e) { System.out.println("输入的不是一个有效的整数!"); } scanner.close(); } }
In the above example, we use the try-catch statement to catch the InputMismatchException exception. If the user input is not a valid integer, the program will catch the exception and output the corresponding prompt information.
In addition to integers, Java programs sometimes need to read floating point numbers, long integers, short integers, etc. Other data types. The following is a sample code that demonstrates how to use the Scanner class to read floating point numbers:
import java.util.InputMismatchException; import java.util.Scanner; public class NumberInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print("请输入一个浮点数:"); double num = scanner.nextDouble(); System.out.println("您输入的浮点数是:" + num); } catch (InputMismatchException e) { System.out.println("输入的不是一个有效的浮点数!"); } scanner.close(); } }
In the above example, we use the nextDouble() method to read the floating point number entered by the user. Similarly, handle illegal input situations by catching InputMismatchException exceptions.
Summary
This article introduces some techniques for reading numerical input in Java programming, covering using the Scanner class to read console input, using try-catch to handle input exceptions, and reading floats. Points, etc. By learning these techniques, you can process user-entered numerical data more flexibly and write more robust Java programs. Hope this article helps you!
The above is the detailed content of Numeric input reading skills in Java programming. For more information, please follow other related articles on the PHP Chinese website!