华氏摄氏转换率只有0.0和-0.0 [英] Fahrenheit to Celsius conversion yields only 0.0 and -0.0

查看:147
本文介绍了华氏摄氏转换率只有0.0和-0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第8章我的Java方法的书(方法,构造函数和字段)和我在与我的运动之一的一个问题。

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.

所提供的code是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;
  }
}

它在风格上可能可怕,但我只是想获得一些工作code,进行了近编译器(因此回归0;S这我相当肯定可以在一个更加规避时髦的方式)。

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)= 0。

In integer math (5 / 9) = 0.

和(度 - 32)*(5/9);将始终为0,你需要使用双或东西。

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

这篇关于华氏摄氏转换率只有0.0和-0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆