不能从Double转换为Double错误.我该如何解决这个问题?



我试图创建一个代码,将提示用户输入,并允许他们在不同种类的公制和美国测量之间转换。我以为我完成了,但我遇到了一个无法修复的错误。每次我使用" double。valueof (readconsole。next());"我得到了一个错误,说"类型不匹配:不能从Double转换到Double"。对如何解决这个问题有什么想法吗?如有任何帮助,不胜感激。

我代码:

import java.util.Scanner;
public class MeasurementConversion
{
    public static void main(String[]args)
    {
        Scanner readConsole = new Scanner(System.in);
        String convert;
        double poundAmount;
        double kilogAmount;
        double ounceAmount;
        double gramAmount;
        double feetAmount;
        double meterAmount;
        double mileAmount;
        double kilomAmount;
        System.out.println("Hello, I am a conversion calculator. I can convert pounds to kilograms, ounces to grams,");
        System.out.println("feet to meters, miles to kilometers, and vice versa.");
        System.out.println("What would you like to convert?");
        convert = readConsole.nextLine();
        if (convert.equalsIgnoreCase("pounds to kilograms"))
        {
            System.out.println("Ok, what is the amount of pounds you know?");
            poundAmount = Double.valueOf(readConsole.next());
            kilogAmount = poundAmount * .4536;
            System.out.println("That is " + kilogAmount + " kilograms.");
        }
        if (convert.equalsIgnoreCase("kilograms to pounds"))
        {
            System.out.println("Ok, what is the amount of kilograms you know?");
            kilogAmount = Double.valueOf(readConsole.next());
            poundAmount = kilogAmount * 2.20462;
            System.out.println("That is " + poundAmount + " pounds.");
        }
        if (convert.equalsIgnoreCase("ounces to grams"))
        {
            System.out.println("Ok, what is the amount of ounces you know?");
            ounceAmount = Double.valueOf(readConsole.next());
            gramAmount = ounceAmount * 28.5;
            System.out.println("That is " + gramAmount + " grams.");    
        }
        if (convert.equalsIgnoreCase("grams to ounces"))
        {
            System.out.println("Ok, what is the amount of grams you know?");
            gramAmount = Double.valueOf(readConsole.next());
            ounceAmount = gramAmount * .035274;
            System.out.println("That is " + ounceAmount + " ounces.");
        }
        if (convert.equalsIgnoreCase("feet to meters"))
        {
            System.out.println("Ok, what is the amount of feet you know?");
            feetAmount = Double.valueOf(readConsole.next());
            meterAmount = feetAmount * .3048;
            System.out.println("That is " + meterAmount + " meters.");
        }
        if (convert.equalsIgnoreCase("meters to feet"))
        {
            System.out.println("Ok, what is the amount of meters you know?");
            meterAmount = Double.valueOf(readConsole.next());
            feetAmount = meterAmount * 3.28084;
            System.out.println("That is " + feetAmount + " feet.");
        }
        if (convert.equalsIgnoreCase("miles to kilometers"))
        {
            System.out.println("Ok, what is the amount of miles you know?");
            mileAmount = Double.valueOf(readConsole.next());
            kilomAmount = mileAmount * 1.61;
            System.out.println("That is " + kilomAmount + " kilometers.");
        }
        if (convert.equalsIgnoreCase("kilometers to miles"))
        {
            System.out.println("Ok, what is the amount of kilometers you know?");
            kilomAmount = Double.valueOf(readConsole.next());
            mileAmount = kilomAmount * .621371;
            System.out.println("That is " + mileAmount + " miles.");
        }
    }
}

我不是java专家,但从代码中我看到您的变量是原始类型double,而您的方法调用返回类Double的实例。可以使用double. parsedouble()将String类型转换为double类型。

我认为你在Java 1.4或更低版本中编译,并且你在执行时没有问题,而是在编译过程中。

type mismatch: cannot convert from Double to double.

是编译错误。

Java 1.4及以下版本不支持自动装箱。因此,您键入的自动转换Double -> double在这里是不允许的:

 double poundAmount = Double.valueOf(readConsole.next());

尝试检查使用的Java版本,并至少使用1.5版本的JDK

相关内容

  • 没有找到相关文章

最新更新