好的,这就是我得到的。一切都完全有效,除非我去转换它我的答案标签弹出0.0,所以我知道它至少从某处解析双倍。我只是不知道是否从输入中得到的数字用于将我的变量设置为我的标签的等式。
框:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Prog8Frame extends JFrame {
private JLabel icon;
private JLabel temperLab;
private JLabel fahr;
private static JTextField input;
private JButton convert;
private JLabel answer;
private JLabel degC;
public Prog8Frame() {
super( "Prog 8" );
setLayout (new FlowLayout() );
Icon thermometer = new ImageIcon( getClass().getResource("thermometer.png") );
icon = new JLabel(thermometer);
add(icon);
temperLab = new JLabel( "Temperature" );
add(temperLab);
fahr = new JLabel( "Fahrenheit" );
add(fahr);
input = new JTextField(7);
add(input);
convert = new JButton("Convert");
add(convert);
convert.addActionListener( new ButtonHandler() );
answer = new JLabel();
add(answer);
degC = new JLabel( "degrees Celsius" );
add(degC);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent click) {
String strInput = input.getText();
double fahren = Double.parseDouble(strInput);
double degCel = ((fahren - 32)*(5/9));
answer.setText("" +degCel);
}
}
}
测试仪:
import javax.swing.JFrame;
public class Prog8 {
public static void main(String[] args) {
Prog8Frame Prog8 = new Prog8Frame();
Prog8.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Prog8.setSize(300, 300);
Prog8.setVisible(true);
}
}
答案 0 :(得分:1)
与5/9
有关,而int
正在转换回0
,基本上等同于double degCel = ((fahren - 32)*(5d/9d));
尝试将其更改为...
double
代替。这将导致Java将结果保持为{{1}}
答案 1 :(得分:1)
罪魁祸首是等式。
double degCel = ((fahren - 32)*(5/9));
5/9
将返回0.使用5.0/9
。这将导致双倍价值。
double degCel = ((fahren - 32)*(5.0/9));