Home>Article>Java> Use Java's Scanner.hasNextDouble() function to determine whether the next input is a double-precision floating point number

Use Java's Scanner.hasNextDouble() function to determine whether the next input is a double-precision floating point number

WBOY
WBOY Original
2023-07-26 10:29:09 2394browse

Use java's Scanner.hasNextDouble() function to determine whether the next input is a double-precision floating point number

In Java programming, we often need to obtain input from the user. The Scanner class is a commonly used tool that provides convenient methods to read different types of input. Sometimes, we want to determine whether the input is a double-precision floating point number. In this case, we can use the hasNextDouble() function of the Scanner class to achieve this.

The hasNextDouble() function is a method provided by the Scanner class. It returns a Boolean value that indicates whether the next input is a double-precision floating point number. Returns true if the input is a double precision floating point number, false otherwise. This function is very convenient and can be used to verify the legality of user input and ensure that we get the expected data type.

The following is a simple sample code that demonstrates how to use the hasNextDouble() function to determine whether the input is a double-precision floating point number:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个双精度浮点数: "); if (scanner.hasNextDouble()) { double number = scanner.nextDouble(); System.out.println("输入的双精度浮点数是: " + number); } else { System.out.println("输入不是一个双精度浮点数"); } scanner.close(); } }

In the above code, we first create a Scanner Object used to read user input. Then, determine whether the next input is a double-precision floating point number by calling the hasNextDouble() function. If the return value is true, call the nextDouble() function to obtain the input double-precision floating point number and assign it to the variable number. Finally, output the resulting double-precision floating point number.

When running the above code, we can see the following running results:

请输入一个双精度浮点数: 3.14 输入的双精度浮点数是: 3.14
请输入一个双精度浮点数: abc 输入不是一个双精度浮点数

It can be seen from the above running results that when we input a double-precision floating point number, the program will be correct The input is judged to be a double-precision floating point number and output. When we input a non-double-precision floating-point number, the program will correctly determine that the input is not a double-precision floating-point number and give a corresponding prompt.

By using the hasNextDouble() function of the Scanner class, we can easily determine whether the input is a double-precision floating point number in Java programming. This function provides a simple way to verify the validity of user input and ensure that we are getting the correct data type. I hope this article will help you understand and use the hasNextDouble() function.

The above is the detailed content of Use Java's Scanner.hasNextDouble() function to determine whether the next input is a double-precision floating point number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn