华氏到摄氏的转换只产生 0.0 和 -0.0 [英] Fahrenheit to Celsius conversion yields only 0.0 and -0.0
问题描述
我正在阅读 Java 方法书的第 8 章(方法、构造函数和字段),但我的一个练习有问题.
I'm on the 8th chapter (Methods, Constructors, and Fields) of my Java methods book and I'm having a problem with one of my exercises.
提供的代码是Temperature.java
The provided code is Temperature.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Temperature extends JApplet
implements ActionListener {
private JTextField displayF, displayC;
private static DecimalFormat displayFormat = new DecimalFormat("0.0");
public void init() {
Container c = getContentPane();
c.setBackground(Color.white);
c.setLayout(new GridLayout(2, 2, 10, 0));
c.add(new JLabel(" Fahrenheit:"));
c.add(new JLabel(" Celsius:"));
displayF = new JTextField(6);
displayF.setBackground(Color.yellow);
displayF.addActionListener(this);
c.add(displayF);
displayC = new JTextField(6);
displayC.setBackground(Color.yellow);
displayC.addActionListener(this);
c.add(displayC);
}
public void actionPerformed(ActionEvent e) {
String s;
double degrees;
FCConverter fc = new FCConverter();
if ((JTextField) e.getSource() == displayF) {
s = displayF.getText().trim();
degrees = Double.parseDouble(s);
fc.setFahrenheit(degrees);
degrees = fc.getCelsius();
displayC.setText(displayFormat.format(degrees));
} else {
s = displayC.getText().trim();
degrees = Double.parseDouble(s);
fc.setCelsius(degrees);
degrees = fc.getFahrenheit();
displayF.setText(displayFormat.format(degrees));
}
}
}
然后我应该编写和测试 FCConverter.java我想出了:
And from there I'm supposed to write and test FCConverter.java I've come up with:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class FCConverter
{
private double Celsius;
private double Fahrenheit;
public double setFahrenheit (double degrees)
{
Celsius = (degrees - 32) * (5 / 9);
return 0;
}
public double getCelsius ()
{
return Celsius;
}
public double setCelsius (double degrees)
{
Fahrenheit = (degrees * 9 / 5 + 32);
return 0;
}
public double getFahrenheit ()
{
return Fahrenheit;
}
}
这在风格方面可能很糟糕,但我只是想获得一些通过编译器的工作代码(因此我相当肯定可以以更时尚的方式规避return 0;").
It's probably horrendous in terms of style, but I just wanted to get some working code that got past the compiler (hence the "return 0;"s which I'm fairly certain can be circumvented in a much more fashionable manner).
我想知道的是,当我在 appletviewer 或浏览器形式中测试时,摄氏到华氏的转换工作正常,但华氏到摄氏的转换仅产生 0.0 和 -0.0.我不明白出了什么问题,因为我设计的部分看起来相当对称.不应该是两种转换方法都有效吗?
What I want to know is, when I test this in appletviewer or in browser form, the Celsius to Fahrenheit conversion works fine, but the Fahrenheit to Celsius conversion yields only 0.0 and -0.0. I don't understand what's wrong since the part I've designed seems fairly symmetrical. Shouldn't it be the case that both or neither conversion method works?
推荐答案
问题好像是 5/9.它们被视为一个整数,5/9 是 0.something 是 0 作为整数.所以无论如何 * 0 = +-0 :)
The problem seems to be 5/9. They are seen as an integer, and 5/9 is 0.something which is 0 as an integer. So whatever * 0 = +-0 :)
在数字后放置一个d",(5d/9d) 迫使编译器将它们视为双倍.
Place a "d" after the numbers, (5d/9d) forcing the compiler to consider them doubles.
这篇关于华氏到摄氏的转换只产生 0.0 和 -0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!