Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

java - Fahrenheit to Celsius conversion yields only 0.0 and -0.0

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.

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));
          }
      }
  }

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;
  }
}

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).

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In integer math (5 / 9) = 0.

and (degrees - 32) * (5 / 9); will always be 0 you need to use doubles or something.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...