输入到输出与java中if / else语句中详细说明的内容不匹配

时间:2014-07-11 01:03:57

标签: java user-interface if-statement jframe

我正在尝试创建一个简单的理想体重计算器。它接受两种类型的输入,除了你的名字 - 公制和英制输入,分别是公斤和英镑。为了指定以下字段是指定kg还是磅,我有一个用于公制或英制的文本输入,其中通过键入m / M或i / I,您可以在公制和英制之间选择。

看起来像这样。

Name: ________
(M)etric or (I)mperial: ________
Weight: _________
      [[Calculate]]

以下是代码:

private void btnInputActionPerformed(java.awt.event.ActionEvent evt){

//Acquire name string info
    String name = this.txtInput_name.getText();

//Aquire system of measurement info
    String SOM = this.txtInput_som.getText();

//Acquire height information
    String heightInput = this.txtInput_height.getText();
    double height = Double.parseDouble(heightInput);

    //Set up if statement to deal with I, M, or neither, if user inputs something else
    //For imperial (designation i, I):
    if ((SOM == "I") || (SOM == "i")) {
    //Imperial: Weight (pounds) = height (inches) × height (inches) × 25 ÷ 703    
        double weightlb = Math.pow(height, 2)*25/703;
        this.lblOutput.setText(name + "'s ideal weight is " + weightlb + " pounds.");
    }

    //For metric (designation m, M):
    else if ((SOM == "M") || (SOM == "m")){
    //Metric:Weight (kg) = height (metres) × height (metres) × 25
        double weightkg = Math.pow(height, 2)*25;
        this.lblOutput.setText(name + "'s ideal weight is " + weightkg + "kg.");
    }

    //For user not inputting any taken designation for metric or imperial as above (e.g. "Celsius", "g", "turkey", etc.)
    else {
        //Not going to tell them about other ways of designating metric/imperial, label text too long.
        this.lblOutput.setText("Please input a valid system of measurement, designation: I or M.");
    }


}  

问题在于,尽管为Metric或Imperial输入了M / m / i / I(一次一个!),它总是吐出else语句的结果 - 请输入一个有效的测量系统。 ..

我是否对if / else if语句的条件做错了,还是完全不同意?

0 个答案:

没有答案