- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
很抱歉我的初学者错误,但我正在编写一个计算器,将华氏温度转换为摄氏度,将摄氏度转换为华氏温度,但我不断收到此错误:
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/
如何在 NSString 中表示 Degree Fahrenheit 符号?下面的代码将产生一个度数符号,然后是大写字母 F,但是是否有实际的华氏温度本身?同样,是否有摄氏度? NSString *f
有没有办法将小圆形度数符号包含到 TextView 中?这将用于温度读数,如摄氏度或华氏度。我想知道是否有人以前以编程方式完成过此操作。 最佳答案 有一个可以在 Java 中使用的摄氏度的 Unico
我正在研究一个帮助我在单位之间进行转换的类(class),我需要询问创建此作品的最佳方法。 这是我关于温度的示例代码: public class Temperature { p
我有一个包含大量食谱的简单网络应用程序,我正在努力让使用屏幕阅读器的人可以访问它。 cooking 温度通常简单地表示为 C(摄氏度)和 F(华氏度)。 屏幕阅读器将这些阅读为字母,我认为这有点令人困
iOS 10 增加了用户在“设置”>“常规”>“语言和区域”>“温度单位”下设置“温度单位”选项的功能。 我的应用如何以编程方式确定此设置,以便它可以显示正确的温度单位?我翻遍了 NSLocale.h
iOS 10 增加了用户在“设置”>“通用”>“语言与地区”>“温度单位”下设置“温度单位”选项的功能。 我的应用如何以编程方式确定此设置,以便显示正确的温度单位?我浏览了 NSLocale.h,没有
我是一名优秀的程序员,十分优秀!