728x90
반응형
돈의 액수를 입력 받아 오만원권, 만원권, 천원권, 500원짜리 동전, 100원 짜리 동전, 1원 짜리 동전이 각각 몇개인지 출력하는 동전게수기 프로그램을 작성하시오
01. 계산식 이용
import java.util.Scanner;
public class Ex05 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.print("돈의 액수를 입력하세요 >> ");
int money = stdin.nextInt();
int won50000 = money/50000;
int moneyCir = money%50000;
int won10000 = moneyCir/10000;
moneyCir = money%10000;
int won1000 = moneyCir/1000;
moneyCir = moneyCir%1000;
int won500 = moneyCir/500;
moneyCir = moneyCir%500;
int won100 = moneyCir/100;
moneyCir = moneyCir%100;
int won50 = moneyCir/50;
moneyCir = moneyCir%50;
int won10 = moneyCir/10;
int won1 = moneyCir%10;
System.out.println("오만원 " + won50000 + "개");
System.out.println("만원 " + won10000 + "개");
System.out.println("천원 " + won1000 + "개");
System.out.println("500원 " + won500 + "개");
System.out.println("100원 " + won100 + "개");
System.out.println("50원 " + won50 + "개");
System.out.println("10원 " + won100 + "개");
System.out.println("1원 " + won1 + "개");
}
}
02. Array 활용
훨씬 간결하게 만들 수 있다.
import java.util.Scanner;
public class ChangeMoney {
public static void main(String[] args) {
int unit[] = {50000,10000,1000,500,100,50,10,1}; //환산할 돈의 종류
System.out.print("환전할 금액을 입력하세요 >> ");
Scanner sc = new Scanner(System.in);
int money = sc.nextInt();
for(int i=0; i<unit.length; i++) {
int res = money/unit[i]; //res는 몫
if(res > 0) {
System.out.println(unit[i] + "원 : " + res + "개");
money = money % unit[i]; //money 갱신
}
}
}
}
03. JFrame
JFrame을 사용하여 구형하고 체크박스를 추가하여 사용할 동전을 사용할 수 있게 구현 할 것
package Ex02;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxCalc2 extends JFrame{
public CheckBoxCalc2() {
setContentPane(new MyPanel());
setSize(300,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class MyPanel extends JPanel{
private String[] names = {"오만원", "만원", "천원", "500원", "100원", "50원", "10원", "1원"};
private int[] price = {50000, 10000, 1000, 500, 100, 50, 10, 1};
private JTextField source;
private JTextField [] tf = new JTextField[8];
private JCheckBox [] jcb = new JCheckBox[7];
public MyPanel(){
setBackground(Color.pink);
setLayout(null);
//금액레이블
JLabel la = new JLabel("금액");
la.setHorizontalAlignment(JLabel.RIGHT);
la.setSize(50, 20);
la.setLocation(20, 20);
add(la);
//금액 입력하는 JTextField
source = new JTextField(30);
source.setSize(100, 20);
source.setLocation(100, 20);
add(source);
//계산 JButton
JButton calcBt = new JButton("계산");
calcBt.setSize(70, 20);
calcBt.setLocation(210, 20);
add(calcBt);
for(int i=0; i<names.length; i++) {
la = new JLabel(names[i]);
la.setHorizontalAlignment(JLabel.RIGHT);
la.setSize(50, 20);
la.setLocation(50, 50+i*20);
add(la);
tf[i] = new JTextField(30);
tf[i].setHorizontalAlignment(JTextField.CENTER);
tf[i].setSize(70, 20);
tf[i].setLocation(120, 50+i*20);
add(tf[i]);
}
for(int i=0; i<names.length-1; i++) {
jcb[i] = new JCheckBox("", true);//초기에 모두 선택
jcb[i].setOpaque(false);
jcb[i].setSize(30, 20);
jcb[i].setLocation(200, 50+i*20);
add(jcb[i]);
}
//계산버튼의 액션 리스터
calcBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = source.getText();//입력된 금액 문자열을 가여옴
if(str.length()==0) { //입력된 금액이 없으면
return;
}
int money = Integer.parseInt(str); //입력된 금액 문자열을 정수로 변환
int res;
for(int i=0; i<price.length; i++) {
if(i==price.length-1) { // 1원 짜리 계산하는 경우
res = money/price[i];
tf[i].setText(Integer.toString(res));
break;
}
if(!jcb[i].isSelected()) {
tf[i].setText("0");
continue;
}
res = money/price[i];
tf[i].setText(Integer.toString(res));
if(res > 0) {
money = money%price[i];
}
}
}
});
}
}
public static void main(String[] args) {
new CheckBoxCalc2();
}
}
728x90
반응형
'JAVA > 예제' 카테고리의 다른 글
[JAVA] UP&DOWN 게임 (0) | 2022.10.05 |
---|---|
[JAVA] 369게임 (0) | 2022.09.16 |
[JAVA] 자릿수 비교 (0) | 2022.09.16 |
[JAVA] 환율계산 (0) | 2022.09.16 |
[JAVA] BMI계산 프로그램 (0) | 2022.09.15 |
댓글