[Java] mini test : 값의 교환(swap), 거스름돈

Young eee·2022년 12월 20일

Java

목록 보기
3/22
post-thumbnail

📖 값의 교환(swap)

package work01;

import java.util.Scanner;

public class MainClass {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int x, y;

		System.out.print("x = ");
		x = sc.nextInt();
		
		System.out.print("y = ");
		y = sc.nextInt();
		
		int temp;
		
		// 값의 교환(swap)
		temp = x;	//x의 값을 저장해 줄 새로운 변수 선언
		x = y;		//x에 y의 값을 넣은 후
		y = temp;	//다른 변수에 저장한 x의 값을 y에 대입
		
		System.out.println("x = " + x);		//x의 값에 y가
		System.out.println("y = " + y);		//y의 값에 x가
	}
}

📖 거스름돈

package work02;

import java.util.Scanner;

public class MainClass {

	public static void main(String[] args) {
		/*
	 		거스름돈 계산기 만들기
            
	 		지불금액 : 3,210원
	 		본인금액 : 10,000원
		  	
		 	거스름돈 -> ?
		 	
            10000원 -> ?장
		 	5000원  -> ?장
		 	1000원  -> ?장
		 	500원   -> ?개
		 	100원   -> ?개
		 	50원    -> ?개
		 	10원    -> ?개
		 * */
		
		Scanner sc = new Scanner(System.in);	
		
		int payment;
		int due;
		int change;
		
		System.out.print("지불할 금액 >> ");
		payment = sc.nextInt();
		
		System.out.print("낸 금액 >> ");
		due = sc.nextInt();
		
		change = due - payment;
		
		System.out.println("거스름 돈 >> " + change);
		
		int tenThousand = change  / 10000;
		
		int fiveThousand = (change % 10000) / 5000;
		
		int thousand = (change % 5000) / 1000;
		
		int fiveHundred = (change % 1000) / 500;
		
		int hundred = (change % 500) / 100;
		
		int fifty = (change % 100) / 50;
		
		int ten = (change % 50) / 10;
		
		System.out.println("10000원 : " + tenThousand + "장");
		System.out.println("5000원 : " + fiveThousand + "장");
		System.out.println("1000원 : " + thousand + "장");
		System.out.println("500원 : " + fiveHundred + "개");
		System.out.println("100원 : " + hundred + "개");
		System.out.println("50원 : " + fifty + "개");
		System.out.println("10원 : " + ten + "개");

		
		
		
	}

}

0개의 댓글