- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做以下编程练习:Grasshopper Debug.声明如下:
Debug celsius converter
Your friend is traveling abroad to the United States so he wrote a program to convert fahrenheit to celsius. Unfortunately his code has some bugs.
Find the errors in the code to get the celsius converter working properly.
To convert fahrenheit to celsius:
celsius = (fahrenheit - 32) * (5/9)
Remember that typically temperatures in the current weather conditions are given in whole numbers. It is possible for temperature sensors to report temperatures with a higher accuracy such as to the nearest tenth. Instrument error though makes this sort of accuracy unreliable for many types of temperature measuring sensors.
我尝试过以下代码:
public class GrassHopper {
public static String weatherInfo(int temp) {
double c=convertToCelsius(temp);
if (c < 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}
public static double convertToCelsius(int temperature) {
double celsius = (temperature-32)*(5/9.0);
return celsius;
}
}
我们发现了以下案例:
test5
expected:<31.11111111111111[] is above freezing t...> but was:<31.11111111111111[4] is above freezing t...>
我们认为这可能是因为使用了 double。然后我们使用 BigDecimal‽ 编写了相同的解决方案:
import java.math.BigDecimal;
import java.text.*;
public class GrassHopper {
public static String weatherInfo(int temp) {
BigDecimal c=convertToCelsius(BigDecimal.valueOf(temp));
NumberFormat oneDecimal = new DecimalFormat("#0.0");
c=c.toString().contains(".00") ? BigDecimal.valueOf(Double.parseDouble(oneDecimal.format(c))) : c;
if (c.compareTo(BigDecimal.ZERO) < 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}
public static BigDecimal convertToCelsius(BigDecimal temperature) {
BigDecimal celsius = (temperature.subtract(BigDecimal.valueOf(32))).multiply(BigDecimal.valueOf(5/9.0));
return celsius;
}
}
测试结果如下:
test1
expected:<13.333333333333334[] is above freezing t...> but was:<13.333333333333334[4] is above freezing t...>
test5
expected:<-2.222222222222222[3] is freezing tempera...> but was:<-2.222222222222222[4] is freezing tempera...>
要了解发生了什么并尝试解决它,我们已阅读:
https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
How do i compare values of BigInteger to be used as a condition in a loop?
你能帮助我们吗‽
编辑:根据@GovindaSakhare提供的答案,我们可以写:
public class GrassHopper {
public static String weatherInfo(int temp) {
System.out.println("original celsius temp: "+temp);
double c=convertToCelsius(temp);
double roundedValue=roundoff(c,15);
String result = "";
if (roundedValue < 0){
result=roundedValue + " is freezing temperature";
}else{
result=roundedValue + " is above freezing temperature";
}
System.out.println("result: "+result);
return result;
}
public static double convertToCelsius(int temperature) {
double celsius = (temperature-32)*(5/9.0);
return celsius;
}
public static double roundoff(double temperature, int precisionLevel) {
double prec = Math.pow(10, precisionLevel);
return Math.round(temperature * prec) / prec;
}
}
但是,我们如何知道应该使用什么精度级别‽
我问这个问题是因为,通过前面的代码,我们观察到以下行为:
一些测试确实通过了:
test1
Log
temp: 56
result: 13.333333333333334 is above freezing temperature
test2
Log
temp: 23
result: -5.0 is freezing temperature
test4
Log
temp: 5
result: -15.0 is freezing temperature
还有一些没有通过:
test3
original celsius temp: temp: 33
expected:<0.55555555555555[5]6 is above freezing ...> but was:<0.55555555555555[]6 is above freezing ...>
test5
original celsius temp: 54
expected:<12.22222222222222[1] is above freezing t...> but was:<12.22222222222222[3] is above freezing t...>
我们认为它必须与最后一个练习的陈述部分相关:
"...Remember that typically temperatures in the current weather conditions are given in whole numbers. It is possible for temperature sensors to report temperatures with a higher accuracy such as to the nearest tenth. Instrument error though makes this sort of accuracy unreliable for many types of temperature measuring sensors. "
你会如何思考和行动来解决这个困难‽‽
编辑2:在@GovindaSakhare讨论链接之后,我们发现Java中的测试用例与我们预期的不同。我们尝试了用户 vztot 给出的提示,我们使用了 MathContext.DECIMAL128
我们尝试了以下方法:
import java.math.*;
import java.text.*;
public class GrassHopper {
public static String weatherInfo(int temp) {
BigDecimal c=convertToCelsius(BigDecimal.valueOf(temp));
if (c.compareTo(BigDecimal.ZERO) <= 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}
public static BigDecimal convertToCelsius(BigDecimal temperature) {
BigDecimal division = BigDecimal.valueOf(5).divide(BigDecimal.valueOf(9.0),MathContext.DECIMAL128);
BigDecimal subtraction = temperature.subtract(BigDecimal.valueOf(32),MathContext.DECIMAL128);
BigDecimal celsius = subtraction.multiply(division,MathContext.DECIMAL128);
return celsius;
}
}
但是它在所有测试中都给出了错误:
test1
expected:<13.33333333333333[4] is above freezing t...> but was:<13.33333333333333[333333333333333333] is above freezing t...>
test2
expected:<-5.0[] is freezing tempera...> but was:<-5.0[00000000000000000000000000000000] is freezing tempera...>
test3
expected:<0.555555555555555[]6 is above freezing ...> but was:<0.555555555555555[555555555555555555]6 is above freezing ...>
test4
expected:<-15.0[] is freezing tempera...> but was:<-15.0[0000000000000000000000000000000] is freezing tempera...>
test5
expected:<34.44444444444444[] is above freezing t...> but was:<34.44444444444444[444444444444444445] is above freezing t...>
所以,我们决定更好地思考,我们应该使用 double 作为输入参数而不是 int:
public class GrassHopper {
public static String weatherInfo /*🌡️ℹ️*/ (double temp) {
double c = convertToCelsius(temp);
if (c <= 0)
return (c + " is freezing temperature");
else
return (c + " is above freezing temperature");
}
public static double convertToCelsius(double temperature) {
return (temperature - 32) * 5/9.0;
}
}
因此,最后一个版本确实通过了测试。
最佳答案
您可以使用org.apache.commons.math3.util.Precision.equals()
来自 Commons Math3。
关于java - 考虑最接近的十分之一将华氏度计算为摄氏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60472822/
如何在 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,没有
我是一名优秀的程序员,十分优秀!