无法在Android模拟器中从Celcius转换为Fahrenheit

时间:2016-02-03 10:27:31

标签: android if-statement

当我从华氏温度转换为摄氏温度时它起作用但当我试图将它从摄氏温度转换回华氏温度时它不起作用。我已经尝试了很多次,但它仍然不适合我。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.converter_second);

    rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
    rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
    btn_convert = (Button) findViewById(R.id.button3_convert);
    btn_clear = (Button) findViewById(R.id.button5_clear);
    edTxt_amount = (EditText) findViewById(R.id.editText_amount);
    txtVw_answer = (TextView) findViewById(R.id.textView7_answer);

    btn_convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double dblFahrenheit = 0;
            double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
            double dblConvertedTemp = 0;
            double dblFahConversion;
            if (v == btn_convert) {
                if (rb_celcius.isChecked()) {
                    String strFah = edTxt_amount.getText().toString();
                    if (!strFah.isEmpty()) {
                        {
                            dblFahrenheit = Double.parseDouble(strFah);
                        }
                        if (dblFahrenheit <= 212) {
                            dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
                            txtVw_answer.setText("The answer is " + dblConvertedTemp);

                        } if (rb_fahrenheit.isChecked()) {
                            String strCel = edTxt_amount.getText().toString();
                            if (!strCel.isEmpty()) {
                                {
                                    dblCelcius = Double.parseDouble(strCel);
                                }
                                if (dblCelcius <= 100) {
                                    dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
                                    txtVw_answer.setText("The answer is" + dblFahConversion);
                                }
                            }
                            else if (v == btn_clear){
                                rg_group.clearCheck();
                                txtVw_answer.setText("");
                        }

                    }
                }
            }
        }

        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

我不能说逻辑是否正确,但从我看到的情况来看,大括号都是错的。这也不是编写此解决方案的最佳方式,我会留给您。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.converter_second);

    rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
    rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
    btn_convert = (Button) findViewById(R.id.button3_convert);
    btn_clear = (Button) findViewById(R.id.button5_clear);
    edTxt_amount = (EditText) findViewById(R.id.editText_amount);
    txtVw_answer = (TextView) findViewById(R.id.textView7_answer);

    btn_convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double dblFahrenheit = 0;
            double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
            double dblConvertedTemp = 0;
            double dblFahConversion;
            if (v == btn_convert) {
                if (rb_celcius.isChecked()) {
                    String strFah = edTxt_amount.getText().toString();
                    if (!strFah.isEmpty()) {
                            dblFahrenheit = Double.parseDouble(strFah);
                    }
                    if (dblFahrenheit <= 212) {
                        dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
                        txtVw_answer.setText("The answer is " + dblConvertedTemp);

                    }
                }

                if (rb_fahrenheit.isChecked()) {
                        String strCel = edTxt_amount.getText().toString();
                        if (!strCel.isEmpty()) {
                                dblCelcius = Double.parseDouble(strCel);
                        }
                        if (dblCelcius <= 100) {
                            dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
                            txtVw_answer.setText("The answer is" + dblFahConversion);
                        } else if (v == btn_clear){
                            rg_group.clearCheck();
                            txtVw_answer.setText("");
                        }

                }
            }
        }

        }
    });
}

答案 1 :(得分:0)

您的缩进和使用大括号使得很难看到问题。如果我修复了缩进,我会得到以下代码:

public void onClick(View v) {

double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
    if (rb_celcius.isChecked()) {
        String strFah = edTxt_amount.getText().toString();
        if (!strFah.isEmpty()) {
            {
                dblFahrenheit = Double.parseDouble(strFah);
            }
            if (dblFahrenheit <= 212) {
                dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
                txtVw_answer.setText("The answer is " + dblConvertedTemp);

            } 
            if (rb_fahrenheit.isChecked()) {
                String strCel = edTxt_amount.getText().toString();
                if (!strCel.isEmpty()) {
                    {
                        dblCelcius = Double.parseDouble(strCel);
                    }
                    if (dblCelcius <= 100) {
                        dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
                        txtVw_answer.setText("The answer is" + dblFahConversion);
                    }
                }
                else if (v == btn_clear){
                    rg_group.clearCheck();
                    txtVw_answer.setText("");
                }

            }
        }
    }
}
}

我认为它不起作用,因为您检查在检查rb_celcius时执行的if块中是否检查了rb_fahrenheit。鉴于这些视图是无线电按钮,我假设它们位于同一个RadioGroup中,在这种情况下必须检查两个无线电按钮。对于RadioGroup来说这是不可能的。这解释了为什么反过来不起作用。从它的外观来看,代码本身应该可以工作。