화폐매수 구하기?
126500 의 금액을 한국 화폐로 바꾸었을 때 각각 몇 개의 화폐가 필요한지 계산해서 출력하라.
class Pad {
public static void main(String[] args) {
Counter c = new Counter();
c.enterAmount(126500);
c.result();
}
}
class Counter {
public int amount = 0;
public int oman = 0;
public int man = 0;
public int ocheon = 0;
public int cheon = 0;
public int obaek = 0;
public int baek = 0;
public int osip = 0;
public int sip = 0;
void enterAmount(int amount) {
this.amount = amount;
}
String counter(int type, int size) {
while (this.amount >= size) {
this.amount -= size;
type++;
}
if (String.valueOf(size).length() == 4) {
return " " + size + "원 " + type + "장";
} else if (String.valueOf(size).length() == 3) {
return " " + size + "원 " + type + "개";
} else if (String.valueOf(size).length() == 2) {
return " " + size + "원 " + type + "개";
} else {
return " " + size + "원 " + type + "장";
}
}
void result() {
System.out.println("금액 : " + this.amount + "원" + "\n" + this.counter(this.oman, 50000) + "\n"
+ this.counter(this.man, 10000) + "\n" + this.counter(this.ocheon, 5000) + "\n"
+ this.counter(this.cheon, 1000) + "\n" + this.counter(this.obaek, 500) + "\n"
+ this.counter(this.baek, 100) + "\n" + this.counter(this.osip, 50) + "\n"
+ this.counter(this.sip, 10));
}
}
댓글
댓글 쓰기