分析大写时出错,但分析小写java时无错误
所以我的代码是一个程序,用来判断水在特定温度下是气体还是固体,然后是单位(华氏度或摄氏度) 该代码适用于单位“F”、“F”、“c”,但不适用于大写字母“c”分析大写时出错,但分析小写java时无错误,java,parsing,Java,Parsing,所以我的代码是一个程序,用来判断水在特定温度下是气体还是固体,然后是单位(华氏度或摄氏度) 该代码适用于单位“F”、“F”、“c”,但不适用于大写字母“c” public static void stateOfWater() { Scanner inputTempString = new Scanner(System.in); System.out.print("Enter temperature: "); String temperatureString = i
public static void stateOfWater() {
Scanner inputTempString = new Scanner(System.in);
System.out.print("Enter temperature: ");
String temperatureString = inputTempString.nextLine().trim();
String unit = temperatureString.substring(temperatureString.length()-1);
unit = unit.toLowerCase();
temperatureString = temperatureString.replace(unit,"");
double temperature = Double.parseDouble(temperatureString);
if ( (temperature <= 0 && unit.equals("c")) || (temperature <= 32 && unit.equals("f")) ) {
System.out.println("The state of the water is: solid (ice). ");
}
if ( ( (0.0 <= temperature && temperature < 100) && unit.equals("c") ) || (((32 <= temperature && temperature < 212) && unit.equals("f")))) {
System.out.println("The state of the water is: liquid. ");
}
if ((temperature > 100 && unit.equals("c")) || (temperature > 212 && unit.equals("f"))) {
System.out.println("The state of the water is: gaseous. ");
}
}
为什么会出现此异常? 将单位转换为小写,然后用空字符串替换小写字符串。如果您的字符串是
43C
,它将尝试用空字符串替换c
。。。那当然不行
您应该首先从温度字符串中删除该单位,然后将该单位转换为小写
String input = inputTempString.nextLine().trim();
String unit = input.substring(input.length() - 1).toLowercase();
double temperature = Double.parseDouble(input.substring(0, input.length() - 1));
但是为什么43F
有效,而43C
无效?
它实际上适用于大写字母F的原因是字母F是浮点文字的一部分,正如文档中所提到的,它反过来指,它反过来指,它将此标记称为FloatTypeSuffix
标记
此外,我建议采用不同的方法 我将解析输入并将其转换为标准单位,例如摄氏度,然后对于负责打印水状态的代码,我只需编写带有一个或两个比较的
if
语句。例如,代替您的if
语句,例如:
(((0.0 <= temperature && temperature < 100) && unit.equals("c") ) || (((32 <= temperature && temperature < 212) && unit.equals("f"))))
(((0.0实际上,您甚至不需要单位!只需检查给定字符串的最后一个字符,如果它是“F”或“F”,则减去32
以摄氏度表示
public static void stateOfWater() {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Enter temperature (e.g. 35F or 35C): ");
String str = scan.nextLine().trim();
double degree = Double.parseDouble(str.substring(0, str.length() - 1));
degree = Character.toUpperCase(str.charAt(str.length() - 1)) == 'F' ? degree : ((degree - 32) * 5) / 9;
String state = Double.compare(degree, 0) <= 0 ? "solid (ice)" : Double.compare(degree, 100) < 0 ? "liquid" : "gaseous";
System.out.println("The state of the water is: " + state + '.');
}
}
public static void stateOfWater(){
尝试(扫描仪扫描=新扫描仪(System.in)){
系统输出打印(“输入温度(如35F或35C):”;
String str=scan.nextLine().trim();
double degree=double.parseDouble(str.substring(0,str.length()-1));
度=Character.toUpperCase(str.charAt(str.length()-1))='F'?度:((度-32)*5)/9;
字符串状态=双精度。比较(度,0)输入字符串是什么?您对问题的分析不正确。您正在用“”替换“c”以获取温度。因此“43C”将生成“43C”,并且无法转换为数字。有问题的代码是unit=unit.toLowerCase();temperatureString=temperatureString.replace(unit)”)
对于输入43C
,unit
变成c
,但是你试图替换掉c
,它甚至不存在于temperatureString
中。我建议首先将整个temperatureString
小写,然后切掉单位字符。我怀疑它是否适用于大写的F
格式现在,因为它有一个相同的问题,即您试图从仍然大写的温度限制中删除小写的单位
。这会产生错误的结果。不仅这些刻度之间的截距不同,斜率也不同。@mc谢谢,忘了它。修复了。谢谢!Mts Mts
String state = "";
if (temperature < 0) {
state = "solid (ice)";
}
else if (temperature < 100) {
state = "liquid";
}
else {
state = "gaseous";
}
public static void stateOfWater() {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Enter temperature (e.g. 35F or 35C): ");
String str = scan.nextLine().trim();
double degree = Double.parseDouble(str.substring(0, str.length() - 1));
degree = Character.toUpperCase(str.charAt(str.length() - 1)) == 'F' ? degree : ((degree - 32) * 5) / 9;
String state = Double.compare(degree, 0) <= 0 ? "solid (ice)" : Double.compare(degree, 100) < 0 ? "liquid" : "gaseous";
System.out.println("The state of the water is: " + state + '.');
}
}