Home  >  Article  >  Java  >  How to read a floating point number from user input using nextDouble() method of Scanner class

How to read a floating point number from user input using nextDouble() method of Scanner class

WBOY
WBOYOriginal
2023-07-24 20:27:222345browse

How to use the nextDouble() method of the Scanner class to read floating point numbers from user input

In Java, the Scanner class is a very commonly used class for reading data from user input. The Scanner class provides many different methods to read different types of data. Among them, the nextDouble() method can be used to read floating point numbers.

The following is a simple sample code that shows how to use the nextDouble() method of the Scanner class to read floating point numbers from user input and perform basic calculations:

import java.util.Scanner;

public class ReadDoubleFromUser {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入第一个浮点数: ");
        double num1 = scanner.nextDouble();

        System.out.print("请输入第二个浮点数: ");
        double num2 = scanner.nextDouble();

        // 进行一些基本的计算
        double sum = num1 + num2;
        double difference = num1 - num2;
        double product = num1 * num2;
        double quotient = num1 / num2;

        System.out.println("两个浮点数的和是: " + sum);
        System.out.println("两个浮点数的差是: " + difference);
        System.out.println("两个浮点数的积是: " + product);
        System.out.println("两个浮点数的商是: " + quotient);

        scanner.close();
    }
}

In this example In the code, a Scanner object is first created to accept user input. Next, the nextDouble() method is used twice to read the two floating point numbers input by the user. Then, perform basic calculations on these two floating point numbers and print the results.

Please note that when using the nextDouble() method, the user needs to enter a floating point number, and the input needs to be legal, that is, it only contains numbers and a decimal point, and cannot contain other illegal characters.

In addition, in actual use, we should error handle user input. Since the nextDouble() method throws an InputMismatchException, we can use the try-catch statement to catch and handle this exception to ensure that our program does not crash when the user enters illegal data.

To summarize, it is very simple to read floating point numbers from user input using the nextDouble() method of the Scanner class. We only need to create a Scanner object, and then use the nextDouble() method to obtain user input, and then save the floating point number into a variable for subsequent processing and calculations.

The above is the detailed content of How to read a floating point number from user input using nextDouble() method of Scanner class. 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