如何让java正确地检测输入?

如何让java正确地检测输入?,java,Java,当我在下面的代码中更改列表选项时,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

当我在下面的代码中更改列表选项时,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意识到我更改了列表选择?
我测试的一个例子是,我将选择设置为“从华氏温度”并键入32,但它给了我14.22221摄氏度和-258.92777775开尔文。

它应该是+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.");
}
  • 首先,这里的计算是错误的

    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(选择等于(“从华氏温度”)){ inC=toConvert-32/1.8; 墨水=(转化率-32/1.8)+273.15; println(“以摄氏度为单位,+toConvert+”华氏度将是“+inC+”摄氏度”); println(“以开氏度为单位,+toConvert+”华氏度为“+inK+”度”); }


这是什么奇怪的事情?可能的复制品仍然没有区别。“我应该换点别的吗?”TessellatingPi,你们能解释一下这个问题吗?输出对meSorry来说似乎很好,但我并没有用括号来表示什么是基于顺序的operations@TessellatingPi可以希望有帮助:)