基本的Android小费计算器

时间:2014-02-26 05:31:35

标签: java android

我正在学习移动应用程序开发课程,其中一个项目是小费计算器。我试图将书中的代码(使用滑块)改为一个带有四个按钮的代码。

此代码编译并运行正常,但是当我点击按钮(15%,18%,20%和22%)时,似乎没有运行计算 - 提示和总金额显示作为0.00和每个人(如果你分开账单,每个人支付的费用)显示为NaN(不是数字。)代码很简单,所以这可能是一个简单的错误......但我不知道它在哪里是老师也不是。我的代码如下。

public class MainActivity extends Activity {

private static final NumberFormat currencyFormat = 
          NumberFormat.getCurrencyInstance();
       private static final NumberFormat percentFormat = 
          NumberFormat.getPercentInstance();
 private double billAmount = 0.0;
 private double diners;
 private double tipPercent;
 private double tipAmount;
 private double totalAmount;
 private double eachPerson;
 private TextView billTV;
 private TextView tipTV;
 private TextView totalTV;
 private TextView eachTV;
 private TextView dinersTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b15= (Button) findViewById(R.id.fifteenButton);
    Button b18= (Button) findViewById(R.id.eighteenButton);
    Button b20= (Button) findViewById(R.id.twentyButton);
    Button b22= (Button) findViewById(R.id.twentytwobutton);
    b15.setOnClickListener(buttonListener);
    b18.setOnClickListener(buttonListener);
    b20.setOnClickListener(buttonListener);
    b22.setOnClickListener(buttonListener);

    billTV = (TextView) findViewById(R.id.billAmount);
    tipTV = (TextView) findViewById(R.id.tipAmount);
    totalTV = (TextView) findViewById(R.id.totalAmount);
    eachTV = (TextView) findViewById(R.id.eachPays);
    dinersTV = (TextView) findViewById(R.id.diners);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public OnClickListener buttonListener = new OnClickListener() 
{

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.fifteenButton :
        tipPercent = .15;

        break;
    case R.id.eighteenButton :
         tipPercent = .18;

        break;
    case R.id.twentyButton :
       tipPercent = .20;

        break;  
    case R.id.twentytwobutton :
        tipPercent = .22;
         break;

    }
    tipAmount = (billAmount * tipPercent );
    totalAmount = billAmount + tipAmount;
    eachPerson = totalAmount / diners;

    tipTV.setText(currencyFormat.format(tipAmount));
    totalTV.setText(currencyFormat.format(totalAmount));
    eachTV.setText(currencyFormat.format(eachPerson));
  }
};
}

2 个答案:

答案 0 :(得分:2)

看起来这是导致问题private double billAmount = 0.0;的行,只为billAmount提供了一个值,

这里例如你可以做什么:

EditText billEditText = (EditText)findViewById(R.id.billEditText);

并在运行时获取值并将其分配给billAmount,

billAmount = Double.parseDouble(billEditText.getText().toString);

然后你做好所有计算。 如果你的意思是billTV = (TextView) findViewById(R.id.billAmount);,那么你将billAmount视为预先提供的,你必须像这样转换它:

billAmount = Double.parseDouble(billTV.getText().toString());

答案 1 :(得分:0)

你的问题是为这条线创造......

private double billAmount = 0.0;

主要是,您将0.0分配给billAmount并且不再更新它,这就是为什么,当您将任意数量乘以它时......结果乘法将为0.0 。因此,更改billAmount的值...以下代码将为您提供正确的结果。

tipAmount = (billAmount * tipPercent );
totalAmount = billAmount + tipAmount;