如何正确地检测java输入?

时间:2014-08-10 10:14:54

标签: java

当我在下面的代码中更改list选项时,java会选择我的输入并给我一个非常奇怪的答案。这是代码:

package tempconverter;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import static java.lang.System.out;
public class TempConverter extends JFrame implements ActionListener {
        final static String[] inList = {"From Celsius", "From Fahrenheit", "From Kelvin"};
        static JFrame f = new JFrame();
        static JTextField enter = new JTextField(3);
        static JButton confirm = new JButton("Convert");
        static JList choose = new JList(inList);
    public static void main(String[] args) {
        confirm.addActionListener(new TempConverter());
        f.setLayout(new FlowLayout());
        f.setSize(300, 60);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(enter); 
        f.add(confirm);
        f.add(choose);
        f.setVisible(true);        
    }
    @Override
    public void actionPerformed (ActionEvent e) {
        Object choice = choose.getSelectedValue();
        double toConvert = Double.parseDouble(enter.getText());
        double inF, inK, inC;
        if (choice.equals("From Celsius")) {
            inF = toConvert * 1.8 + 32;
            inK = toConvert + 273.15;
            out.println("In degrees Fahrenheit, " + toConvert + " degrees Celsius would be " + inF + " degrees.");
            out.println("In degrees Kelvin, " + toConvert + " degrees Celsius would be " + inK + " degrees.");
        }
        if (choice.equals("From Fahrenheit")) {
            inC = toConvert - 32 / 1.8;
            inK = toConvert - 32 / 1.8 - 273.15;
            out.println("In degrees Celsius, " + toConvert + " degrees Fahrenheit would be " + inC + " degrees.");
            out.println("In degrees Kelvin, " + toConvert + " degrees Fahrenheit would be " + inK + " degrees.");
        }
        if (choice.equals("From Kelvin")) {
            inC = toConvert - 273.15;
            inF = inC + 1.8 + 32;
            out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
            out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
        }
    }
}

如何让java意识到我改变了列表选择? 我测试的一个例子是我将选择设置为“From Fahrenheit”并输入32,但它给了我14.22222222222222221摄氏度和-258.92777777777775开氏度。

2 个答案:

答案 0 :(得分:2)

应该是+273.15而不是-273.15,并且错误为*。评论中提到的变更

if (choice.equals("From Fahrenheit")) {
    inC = toConvert - 32 / 1.8;            
    inK = toConvert - 32 / 1.8 + 273.15;
    // change here ............^......

    out.println("In degrees Celsius, " + toConvert
            + " degrees Fahrenheit would be " + inC + " degrees.");
    out.println("In degrees Kelvin, " + toConvert
            + " degrees Fahrenheit would be " + inK + " degrees.");
}

if (choice.equals("From Kelvin")) {
    inC = toConvert - 273.15;
    inF = inC * 1.8 + 32;
    // .......^.... change here

    out.println("In degrees Celsius, " + toConvert
            + " degrees Kelvin would be " + inC + " degrees.");
    out.println("In degrees Fahrenheit, " + toConvert
                + " degrees Kelvin would be " + inF + " degrees.");
}

答案 1 :(得分:0)

  • 首先你的计算错误。

    inF = inC + 1.8 + 32;

  • 这是工作代码。

    if (choice.equals("From Kelvin")) {
    inC = toConvert - 273.15;
    inF = (inC * 1.8) + 32;
    out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
    out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
        }
    

    if(choice.equals(“From Fahrenheit”)){ inC = toConvert - 32 / 1.8; inK =(toConvert - 32 / 1.8)+ 273.15; out.println(“摄氏度”,“+转换+”度华氏度将是“+ inC +”度。“); out.println(“以度开尔文”,“+转换+”度华氏度将是“+ inK +”度。“);     }