我正在制作一个程序将一个温度转换为另一个温度。例如,华氏温度到摄氏度,摄氏温度到华氏温度,摄氏温度到开氏温度等。温度转换中的逻辑错误
我已经设置了GUI,但现在我在转换过程中出现错误。我寻找转换方法,但似乎无法找到任何。所以我根据我在网上看到的公式来写他们。
private static class EventHandler implements ActionListener {
/**
* this method gets called when an object we are listening to is interacted with
*
* @param evt ActionEvent that interacted with
*/
public void actionPerformed(ActionEvent evt) {
//creates the formating we would like for the numbers
DecimalFormat df = new DecimalFormat("#.##");
//if the event triggered was celInput than
if (evt.getSource() == cText) {
String strcText = cText.getText();
double cTemp = Double.parseDouble(strcText);
fText.setText("" + convertCtoF(cTemp));
kText.setText("" + convertCtoK(cTemp));
}else if(evt.getSource() == fText) {
String strfText = fText.getText();
double fTemp = Double.parseDouble(strfText);
cText.setText("" + convertFtoC(fTemp));
kText.setText("" + convertFtoK(fTemp));
}else if(evt.getSource() == kText) {
String strkText = kText.getText();
double kTemp = Double.parseDouble(strkText);
cText.setText("" + convertKtoC(kTemp));
fText.setText("" + convertKtoF(kTemp));
}
}//end actionPerformed method
}
public static double convertCtoF(double c) {
return (c/(5/9)) + 32;
}
public static double convertFtoC(double f) {
return (5/9) * (f - 32);
}
public static double convertCtoK(double c) {
return c + 273.15;
}
public static double convertFtoK(double f) {
return convertFtoC(f) + 273.15;
}
public static double convertKtoC(double k) {
return k - 273.15;
}
public static double convertKtoF(double k) {
return ((9/5) * convertKtoC(k)) + 32;
}
这就是我认为为了找到逻辑错误而需要的所有代码。
任何帮助将不胜感激,因为我不明白为什么会发生这种情况。
你得到了什么错误? –
编译时没有确切的错误。但是当我在转换框中输入27时,我得到270.2(重复)而不是80.6。开尔文获得-2.7(重复)而不是300。 – TaylorTDHouse