【发布时间】:2021-10-24 14:06:21
【问题描述】:
我是Java初学者,请对我好一点。
我有一个任务:排队的人要买票。他们有钱三小费:25,50,100。门票费用 - 25。如果收银员有零钱 - 他会给票,如果没有 - 不。
如果钱箱里有小费25(25+25+25)的钱足够了,但是没有人有小费50,收银员从别人那里拿了100怎么办?
我的代码:
public static void line(Integer... people) {
ArrayList<Integer> cashbox = new ArrayList<>();
for (int money : people) {
if (money == 25) {
cashbox.add(money);
System.out.println("Here is your ticket!");
continue;
} else if (money == 50 && cashbox.contains(25)) {
System.out.println("Here is your ticket!");
cashbox.remove(cashbox.indexOf(25));
continue;
} else if (money == 100 && cashbox.contains(50) && cashbox.contains(25)) {
System.out.println("Here is your ticket!");
cashbox.remove(cashbox.indexOf(50));
cashbox.remove(cashbox.indexOf(25));
continue;
}
else {System.out.println("Sorry, I haven't odd money for you!");}
}
}
【问题讨论】:
标签: java arrays if-statement arraylist