基本的android:switch语句的语法而不是else-if

时间:2011-06-19 23:31:52

标签: java android syntax if-statement

我正在研究一个Android初学者的教程,这是一个小费计算器。它运行正常,但我想知道如何用switch语句替换else-if语句。并不是说它对于这个程序来说非常重要,但我只是试图围绕语法进行思考。

package com.android.tipcalc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.view.View;

public class tipcalc extends Activity
{

    private EditText txtbillamount;
    private EditText txtpeople;
    private RadioGroup radiopercentage;
    private RadioButton radio15;
    private RadioButton radio18;
    private RadioButton radio20;

    private TextView txtperperson;
    private TextView txttipamount;
    private TextView txttotal;

    private Button btncalculate;
    private Button btnreset;

    private double billamount = 0;
    private double percentage = 0;
    private double numofpeople = 0; 
    private double tipamount = 0;
    private double totaltopay = 0;
    private double perperson = 0; 


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initControls();

    }

    private void initControls()
    {
        txtbillamount = (EditText)findViewById(R.id.txtbillamount);
        txtpeople = (EditText)findViewById(R.id.txtpeople);
        radiopercentage = (RadioGroup)findViewById(R.id.radiopercentage);
        radio15 = (RadioButton)findViewById(R.id.radio15);
        radio18 = (RadioButton)findViewById(R.id.radio18);
        radio20 = (RadioButton)findViewById(R.id.radio20);
        txttipamount=(TextView)findViewById(R.id.txttipamount);
        txttotal=(TextView)findViewById(R.id.txttotal);
        txtperperson=(TextView)findViewById(R.id.txtperperson);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        btnreset = (Button)findViewById(R.id.btnreset);

        btncalculate.setOnClickListener(new Button.OnClickListener() {
            public void onClick (View v){ calculate(); }});
        btnreset.setOnClickListener(new Button.OnClickListener() {
            public void onClick (View v){ reset(); }});

    }

    private void calculate()
    {
    billamount=Double.parseDouble(txtbillamount.getText().toString());
    numofpeople=Double.parseDouble(txtpeople.getText().toString());

    if (radio15.isChecked()) {
        percentage = 15.00;
    } else if (radio18.isChecked()) {
        percentage = 18.00;
    } else if (radio20.isChecked()) {
        percentage = 20.00;
    }

    tipamount=(billamount*percentage)/100;
    totaltopay=billamount+tipamount;
    perperson=totaltopay/numofpeople;

    txttipamount.setText(Double.toString(tipamount));
    txttotal.setText(Double.toString(totaltopay));
    txtperperson.setText(Double.toString(perperson));        
    }

    private void reset()
    {
    txtbillamount.setText("");
    txtpeople.setText("");
    radiopercentage.clearCheck();
    radiopercentage.check(R.id.radio15);
    txttipamount.setText("...");
    txttotal.setText("...");
    txtperperson.setText("...");
    }
}

3 个答案:

答案 0 :(得分:5)

我上面的人说的是正确的,但是为了使用切换语句,你可以在你的OnCheckedChangedListener上设置RadioGroup,然后使用这样的类:

private class MyCheckedChangedListener implements OnCheckedChangeListener {

@Override
public void onCheckedChanged( RadioGroup group, int checkedId ) {

    switch (checkedId) {
        case R.id.radio15:
            percentage = 15f;
            break;
        case R.id.radio18:
            percentage = 18f;
            break;
        case R.id.radio20:
            percentage = 20f;
            break;
    }
}

}

答案 1 :(得分:1)

switch用于一个变量 - 即您有x,可以等于3,5或7.然后您switch x并提供几个案例 - 如何处理每个变量可能的值(当没有给定的值匹配时,您也可以使用默认情况)。在您的情况下,您正在检查几个不同的变量,因此if ... else if ... else是正确的方法。当然,您可以将单选框设置为共享变量,然后可以switch

答案 2 :(得分:0)

如果你在谈论calculate()中的if-else语句,你不能直接用switch语句替换它。 switch语句中的case值需要是编译时常量(整数或枚举值)。此外,if-else在这里完美地表达了你想要做的事情的逻辑。

可以基于radio15,radio18和radio20的状态计算“切换测试值”(例如,基于八种可能的值组合,从0到8的整数)和切换,但我强烈建议不要这样做。它不仅会不必要地复杂化和模糊正在发生的事情的逻辑,如果你在忘记这个聪明的伎俩之后六个月需要维护代码,你就会诅咒自己。

相关问题