Java

Java) Calculator 출력 & 편의점 거스름돈 계산

pogun 2024. 12. 26. 13:50

*1번과 2번 모두 제어문을 사용하지 않고 풀이*

1. 두개의 수를 입력 받고 사칙연산에 대해서 출력

Scanner sc = new Scanner(System.in);

int number1, number2;

System.out.println("첫번째 수 : ");
number1 = sc.nextInt();

System.out.println("두번째 수 : ");
number2 = sc.nextInt();

System.out.println("두수의 합 : " + (number1+number2));
System.out.println("두수의 빼기 : " + (number1-number2));
System.out.println("두수의 곱하기 : " + (number1*number2));
System.out.println("두수의 나누기 : " + (number1/number2));
System.out.println("두수의 나머지 : " + (number1%number2));

2. 편의점 거스름돈 계산

조건 :

입력 (지불해야 할 금액)

입력 (자신이 지불한 금액)

거스름돈 (5000원 ?장, 1000원 ?장, 500원 ?개, 100원 ?개, 50원 ?개, 10원 ?개 출력)

int lageMy, meMy;
int textFull, five, four, three, two, one, zero;
System.out.println("지불해야 할 금액 : ");
lageMy = sc.nextInt();

System.out.println("자신이 지불한 금액 : ");
meMy = sc.nextInt();

System.out.println("거스름돈 : " + (meMy - lageMy) + "원");

textFull = meMy - lageMy;
five = (textFull / 5000);
textFull = textFull % 5000;
System.out.println("5000원 : " + five + "장");

four = (textFull / 1000);
textFull = textFull % 1000;
System.out.println("1000원 : " + four + "장");

three = (textFull / 500);
textFull = textFull % 500;
System.out.println("500원 : " + three + "개");

two = (textFull / 100);
textFull = textFull % 100;
System.out.println("100원 : " + two + "개");

one = (textFull / 50);
textFull = textFull % 50;
System.out.println("50원 : " + one + "개");

zero = (textFull / 10);
textFull = textFull % 10;
System.out.println("10원 : " + zero + "개");

textFull % ?? 해주는 이유 : 5000원 몇장 계산 후 나머지를 1000원 몇장으로 넘어가야 함

: 해주지 않으면 거스름돈 전체에서 각각 5000원, 1000원, 500원 갯수를 카운트