写了一段清单,我感觉还是不好:因为钱花得刚好是1元或是5,10,50,100块时,钱包才会把它删除。如果才花7块,钱包里没有一张7块面值的。花10块吧,太亏了。那怎样找回零头3张1块或是6张5毛?(算了吧谁的钱包里会有5毛呀!)请高手赐教代码。我写的如下:
import java.util.*;class Adult {
int totalAccount;
ArrayList wallet=new ArrayList();
public int getTotalAccount() {
return totalAccount;
}
public ArrayList getWallet() {
return wallet;
} public void setWallet( ArrayList aWallet) {
wallet=aWallet;
} public void addMoney(int bill) {
Integer boxedBills=new Integer(bill);
wallet.add(boxedBills);
} public int getMoneyTotal() {
int total=0;
for(Iterator i=wallet.iterator();i.hasNext();) {
Integer s=(Integer)i.next();
int bill=s.intValue();
total+=bill;
totalAccount=total;
}
return totalAccount;
} protected void removeBill(int aBill) {
Iterator i=wallet.iterator();
while(i.hasNext()) {
Integer billBoxed=(Integer)i.next(); if(billBoxed.intValue()==aBill)
i.remove();
}
}
public void spendMoney(int aBill) {
int total;
total=getMoneyTotal();
totalAccount=total-aBill;
removeBill(aBill);
}
}public class Test {
public static void main(String args[]) {
Adult anAdult=new Adult();
anAdult.addMoney(1);
anAdult.addMoney(5);
anAdult.addMoney(10);
anAdult.addMoney(20);
anAdult.addMoney(50);
anAdult.addMoney(100);
System.out.println(anAdult.getWallet());
System.out.println("in total="+anAdult.getMoneyTotal()); System.out.println("Now spend :7¥");
anAdult.spendMoney(7);
System.out.println("The wallet remains:"+anAdult.getTotalAccount());
System.out.println(anAdult.getWallet());
}
}
import java.util.*;class Adult {
int totalAccount;
ArrayList wallet=new ArrayList();
public int getTotalAccount() {
return totalAccount;
}
public ArrayList getWallet() {
return wallet;
} public void setWallet( ArrayList aWallet) {
wallet=aWallet;
} public void addMoney(int bill) {
Integer boxedBills=new Integer(bill);
wallet.add(boxedBills);
} public int getMoneyTotal() {
int total=0;
for(Iterator i=wallet.iterator();i.hasNext();) {
Integer s=(Integer)i.next();
int bill=s.intValue();
total+=bill;
totalAccount=total;
}
return totalAccount;
} protected void removeBill(int aBill) {
Iterator i=wallet.iterator();
while(i.hasNext()) {
Integer billBoxed=(Integer)i.next(); if(billBoxed.intValue()==aBill)
i.remove();
}
}
public void spendMoney(int aBill) {
int total;
total=getMoneyTotal();
totalAccount=total-aBill;
removeBill(aBill);
}
}public class Test {
public static void main(String args[]) {
Adult anAdult=new Adult();
anAdult.addMoney(1);
anAdult.addMoney(5);
anAdult.addMoney(10);
anAdult.addMoney(20);
anAdult.addMoney(50);
anAdult.addMoney(100);
System.out.println(anAdult.getWallet());
System.out.println("in total="+anAdult.getMoneyTotal()); System.out.println("Now spend :7¥");
anAdult.spendMoney(7);
System.out.println("The wallet remains:"+anAdult.getTotalAccount());
System.out.println(anAdult.getWallet());
}
}
楼主【shallendor】截止到2008-07-02 19:26:53的历史汇总数据(不包括此帖):
发帖的总数量:12 发帖的总分数:350
结贴的总数量:7 结贴的总分数:240
无满意结贴数:1 无满意结贴分:20
未结的帖子数:5 未结的总分数:110
结贴的百分比:58.33 % 结分的百分比:68.57 %
无满意结贴率:14.29 % 无满意结分率:8.33 %
楼主加油
2.在public void spendMoney(int aBill) {里面检查wallet里面是否正好凑齐 aBill元钱
我想在集合(WALLET)删除一个元素,如果希望被删除的元素(¥7),但在集合(WALLET)中没有。那么可不可以用集合(WALLET)中的一个元素减掉(¥7),例如(¥10-¥7)。然后将余额¥3(由3个¥1组成)添加到集合(WALLET)中去?
你的集合中的数是能够组合成任一数字的,如果不能组成,那就没法搞了一般是:1 2 2 5,就可以组成1-10之间的任意数算的时候,这样:
List<Integer> 要删除的数列表 = new ArrayList<Integer();
public void 取数(int 要删除的数){
if(要删除的数不存在){
int temp=离这个数最近的数 //小于要取的数()
temp = 要删除的数-temp
取数(temp); //递归
}else{
要删除的数列表.add(要删除的数); //缓存要删除的数
}
}