0

如果语法在这里关闭,我深表歉意,尤其是关于布尔值:

public class WeightConverter extends Activity implements OnClickListener, OnCheckedChangeListener {
    Button convertWeight;
    TextView conversionResults;
    EditText enterWeight;
    RadioGroup weightPicker;
    RadioButton radKG, radLB;
    double weightValue, convertedWeight;
    String weightString;
    String measurement;
    Boolean weightSwitch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weightconverter);
        initializeVariables();
        weightSwitch = false;
        radKG.setChecked(true);
    }

    private void initializeVariables() {
        convertWeight = (Button) findViewById(R.id.btnConvertWeight);
        conversionResults =(TextView) findViewById(R.id.tvWeightConversion);
        enterWeight =(EditText) findViewById(R.id.etWeightToConvert);
        weightPicker = (RadioGroup) findViewById(R.id.rgWeightType);
        radKG = (RadioButton) findViewById(R.id.radKG);
        radLB = (RadioButton) findViewById(R.id.radLB);
        convertWeight.setOnClickListener(this);
        weightPicker.setOnCheckedChangeListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btnConvertWeight:

            if (weightSwitch = true){
                convertedWeight = weightValue /2.2;
                measurement = "Kilograms";
            }else{
                convertedWeight = weightValue *2.2;
                measurement = "Pounds";
            }


            conversionResults.setText(weightValue + " = " + String.valueOf(convertedWeight) + " " + measurement);
            break;
        }

    }

    @Override

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        weightValue = Double.parseDouble(enterWeight.getText().toString());
        switch (checkedId) {

        case R.id.radKG:

            weightSwitch = true;

            break;
        case R.id.radLB:

            weightSwitch = false;

            break;

        }
    }



}

根据用户是否选择任一布尔类型,应执行不同的计算,但只有转换为磅才有效。

4

1 回答 1

0

您正在尝试将boolean值与=此行中的运算符进行比较:

if (weightSwitch = true){

但是=赋值运算符也是如此weightSwitch,总是如此true

用于==比较boolean值或更好:

if (weightSwitch)

因为它已经是一个boolean值。

于 2013-05-08T19:51:25.237 回答