Quantcast
Channel: basic android: syntax for switch statement instead of else-if - Stack Overflow
Viewing all articles
Browse latest Browse all 4

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

$
0
0

I am working on an android beginner's tutorial that is a tip calculator. It runs properly, but I was wondering how to replace the else-if statement with a switch statement. 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("...");    }}

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>