简体   繁体   English

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

[英]basic android: syntax for switch statement instead of else-if

I am working on an android beginner's tutorial that is a tip calculator. 我正在研究一个Android初学者的教程,这是一个小费计算器。 It runs properly, but I was wondering how to replace the else-if statement with a switch statement. 它运行正常,但我想知道如何用switch语句替换else-if语句。 Not that it is that important for the purposes of this program, but I'm just trying to wrap my mind around the syntax. 并不是说它对于这个程序来说非常重要,但我只是试图围绕语法进行思考。

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("...");
    }
}

What the people above me said is correct, but for the sake of using a switch statement for the hell of it, you could set an OnCheckedChangedListener on your RadioGroup , then use a class like this: 我上面的人说的是正确的,但是为了使用切换语句,你可以在你的RadioGroup上设置一个OnCheckedChangedListener ,然后使用这样的类:

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;
    }
}

} }

A switch is used on one variable - ie you have x , which can equal 3,5 or 7. Then you switch x and give several cases - what to do on each possible value (you can also have a default case, when none of the given values match). 一个switch用于一个变量 - 即你有x ,它可以等于3,5或7.然后你switch x并给出几个案例 - 对每个可能的值做什么(你也可以有一个默认情况,当没有给定的值匹配)。 In your case, you're checking several different variables, so if ... else if ... else is the correct method. 在你的情况下,你正在检查几个不同的变量,所以if ... else if ... else是正确的方法。 You can, of course, make the radio boxes set a shared variable, which you can then switch . 当然,您可以将无线电盒设置为共享变量,然后可以switch

If you're talking about the if-else statements in calculate() , you can't replace it directly with a switch statement. 如果你在谈论calculate()的if-else语句,你不能直接用switch语句替换它。 The case values in a switch statement need to be compile-time constants (either integers or enum values). switch语句中的case值需要是编译时常量(整数或枚举值)。 Besides, the if-else here perfectly expresses the logic of what you are trying to do. 此外,if-else在这里完美地表达了你想要做的事情的逻辑。

You could compute a "switch test value" based on the states of radio15, radio18, and radio20 (say, an integer from 0 to 8, based on the eight possible combinations of values) and switch on that, but I would strongly recommend against such an approach. 可以根据radio15,radio18和radio20的状态计算“切换测试值”(例如,基于八种可能的值组合,从0到8的整数)并打开它,但我强烈建议不要这种方法。 Not only would it needlessly complicate and obscure the logic of what's going on, you would be cursing yourself if you needed to maintain the code six months from now after you had forgotten the clever trick. 它不仅会不必要地复杂化并模糊正在发生的事情的逻辑,如果你在忘记聪明的伎俩之后六个月需要维护代码,你就会诅咒自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM