java - 在 Java 中使用扫描仪

标签 java

我正在处理 Java 作业,每当我将小数插入扫描仪时,代码都会返回错误。我已经足够认识到这并不是因为数字是小数,而是因为每当输入任何非数字字符时都会返回此错误。

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at population.main(population.java:14)

如果有人可以帮助我得到小数,那就太酷了,这是我的错误代码。

import java.util.Scanner;

public class population {

    public static void main(String[] args) {
        System.out.print("Enter the amount of years:"); // Prompts the user
        Scanner input = new Scanner(System.in); // Defines the scanner
        double value = input.nextInt(); // Defines the variable


        double A = (60.0 * 24.0 * 365.0); // Brings time from seconds to years
        double B = ((60.0 / 7.0) * A); // Births per year
        double C = ((60.0 / 13.0) * A); // Deaths per year
        double D = ((60.0 / 45.0) * A); // Immigration per year
        double E = (B + D - C); // Change per year  
        double F = ((E * value) + 312032486.0); // Change in population after 5 years

        System.out.println(F);

    }

}

最佳答案

input.nextInt(); 接受一个整数。将其更改为 input.nextDouble()

关于java - 在 Java 中使用扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986739/

相关文章:

java - 如何在Spring注释配置中指定bean的父级?

java - jsp中如何通过session访问对象

java - Java中的随机数

java - 谓词方法 yes/no/maybe 响应

Java 在编辑 observableList 时抛出 IndexOutOfBounds

java - 二叉搜索树的toString方法

java - 快速响应的总线和REST服务解耦?

java - Google pay isReadyToPay 方法返回 false

java - 哪种 toString 技术更有效?

java - 安卓资源管理