java - 摄氏度/华氏度转换错误

标签 java

很抱歉我的初学者错误,但我正在编写一个计算器,将华氏温度转换为摄氏度,将摄氏度转换为华氏温度,但我不断收到此错误:

Exception in thread "main" java.lang.NumberFormatException: 
For input string: "69C"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.valueOf(Double.java:504)
    at Converter.main(Converter.java:39)  

这是我的代码,希望对您有所帮助:

import java.util.Scanner; 
public class Converter {

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

        System.out.println("Welcome to the Fahrenheit/Celsius Converter!");
        System.out.println("To convert, type in your temperature and the
                            identify if you are using fahrenheit or celsius.");
        System.out.println("Example: 69F or 69C F = Fahrenheit C = Celsius");

        String temperature;
        char f = 'f';
        char c = 'c';
        double answer , temp;

        temperature = keyboard.nextLine();            

        if (temperature.contains("F")) {
            temperature = temperature.replace(f , ' ');
            temp = Double.valueOf(temperature);
            answer = temp - 30 / 2;
            System.out.println("Poof! The temperature is " + answer + "C");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("F")) {
            temperature = temperature.replace(f , ' ');
            temp = Double.valueOf(temperature);
            answer = temp - 30 / 2;
            System.out.println("Poof! The temperature is " + answer + "C");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("C")) {
            temperature = temperature.replace(c , ' ');
            temp = Double.valueOf(temperature);
            answer = temp + 30 * 2;
            System.out.println("Poof! The temperature is " + answer + "F");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("c")) {
            temperature = temperature.replace(c , ' ');
            temp = Double.valueOf(temperature);
            answer = temp + 30 * 2;
            System.out.println("Poof! The temperature is " + answer + "F");
        } else {
            System.out.println("You entered the temperature wrong!");
        }
    }
}

最佳答案

你不能将 69C 转换为数字,可以吗。替换区分大小写

尝试

temperature = temperature.replace('C' , ' ');

当然你可以将变量更改为“C”

关于java - 摄氏度/华氏度转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19149929/

相关文章:

java - Java中的单例(线程安全) - 一旦没有人使用该对象,如何销毁成员?

java - hsql Java表列访问问题

java - 如何访问单独函数中的变量 - Android

java - 使用 google texttospeech : Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt 时出错,或 Jetty NPN/ALPN 可用

java - 为什么jmeter记录不捕获Basic Authorization相关的Http headers

java - 将组件值和另一个参数发送给监听器

java - 检测文件或文件夹是否已更新的 Hook 是什么?

java - 无法保存RelativeLayout的状态

java - Adobe Experience Manager 中的用户

java - 非矩形 java UserControl