In my Java Program I have a method:
private void giveCash(int money) {
int banknotes[] = {10, 20, 50, 100, 200, 500};
StringBuffer cash = new StringBuffer();
int i = banknotes.length - 1;
while (i >= 0)
if (banknotes[i] > money)
i--;
else {
cash.append(banknotes[i]).append(" ");
money -= banknotes[i];
}
// display cash in cash field
TextView cash = (TextView) findViewById(R.id.cash);
cash.setText(cash);
}
For, example, if money == 990 the output will be: 500 200 200 50 20 20
How to do the output: 500x1, 200x2, 50x1, 20x2 ?
Aucun commentaire:
Enregistrer un commentaire