多个答案,但我只想要一个

时间:2012-11-16 17:22:12

标签: android

我创建了几个if else语句,同时解析了一个double,然后答案是double =每个else语句。当我运行该程序时,它运行得非常漂亮,除非每个答案都给出了......

//Makes a variable for the entered amount
            double mercurypf;
            double venuspf;
            double earthpf;
            double marspf;
            double jupiterpf;
            double saturnpf;
            double uranuspf;
            double neptunepf;
            double plutopf;


            // constants
            final double mercuryforce = 0.38; 
            final double venusforce = 0.91; 
            final double earthforce = 1.00; 
            final double marsforce = 0.38; 
            final double jupiterforce = 2.34; 
            final double saturnforce = 1.06; 
            final double uranusforce = .92;
            final double neptuneforce = 1.19;
            final double plutoforce = 0.06;



            // Code used to determine which planet RadioButton is checked:

            if(mercury.isChecked())
            {
                mercurypf = mercuryforce * weight;
            }
            else
            {
                mercurypf = 0.00;
            }


            if(venus.isChecked())
            {
                venuspf = venusforce * weight;
            }
            else
            {
                venuspf = 0.00;

            }
            if(earth.isChecked())
            {
                earthpf = earthforce * weight;
            }
            else
            {
                earthpf = 0.00;
            }
            if(mars.isChecked())
            {
                marspf = marsforce * weight;
            }
            else
            {
                marspf = 0.00;
            }
            if(jupiter.isChecked())
            {
                jupiterpf =jupiterforce * weight;
            }
            else
            {
                jupiterpf = 0.00;
            }
            if(saturn.isChecked())
            {
                saturnpf = saturnforce * weight;
            }
            else
            {
                saturnpf = 0.00;
            }

            if(uranus.isChecked())
            {
                uranuspf = uranusforce * weight;
            }
            else
            {
                uranuspf = 0.00;
            }

            if(neptune.isChecked())
            {
                neptunepf = neptuneforce * weight;
            }
            else
            {
                neptunepf = 0.00;
            }

            if(pluto.isChecked())
            {
                plutopf = plutoforce * weight;
            }
            else
            {
                plutopf = 0.00;
            }

            //Creates a textview object, assigns the xml r.id, and then changes the     text to report the amount.
         TextView t = (TextView)findViewById(R.id.ansTextView); 
            t.setText("Your planetary weight is: " + plutopf + neptunepf + uranuspf     + saturnpf + jupiterpf + marspf + earthpf + mercurypf + venuspf);

        }
    }

答案将是0.0 0.0 0.0 9.1 0.0 0.0等....对于除了所选行星之外的所有物品,其余为0.0我知道这是由于MY = mercurypf + plutopf ...有没有办法避免必须这样做,但仍然会返回他们选择的单选按钮的结果?

2 个答案:

答案 0 :(得分:5)

只需将您的结构更改为以下

  // constants
        final double mercuryforce = 0.38; 
        final double venusforce = 0.91; 
        final double earthforce = 1.00; 
        final double marsforce = 0.38; 
        final double jupiterforce = 2.34; 
        final double saturnforce = 1.06; 
        final double uranusforce = .92;
        final double neptuneforce = 1.19;
        final double plutoforce = 0.06;

  TextView t = (TextView)findViewById(R.id.ansTextView); 
  if(uranus.isChecked())
        {
            double uranuspf = uranusforce * weight;
            t.setText("Your planetary weight is: " + uranuspf;
        }
   else if(mercury.isChecked())
        {
            double mercurepf = mercuryforce * weight;
            t.setText("Your planetary weight is: " + mercurypf;
        }
   else if( ...

并继续这样

答案 1 :(得分:1)

更改为以下内容,因为您不关心每个星球上的重量:

  double pf = 0.0;
  TextView t = (TextView)findViewById(R.id.ansTextView); 
  if(mercury.isChecked())
     pf = mercuryforce * weight;
  else if(venus.isChecked())
     pf = venusforce * weight;
  .....
  else if (pluto.isChecked())
     pf = plutoforce * weight;

  t.setText("Your planetary weight is: " + pf);

或者,为了让它更好

double mult = 0.0;
if (mercury.isChecked())
   mult = mercuryForce;
else if (venus.isChecked())
   mult = venusForce;
....
else if (pluto.isChecked())
   mult = plutoForce;

t.setText("Your Planetary weight is: " + mult * weight);
相关问题