본문 바로가기
JAVA/조건식

조건식(if else-if, switch-case)예제

by pms93 2022. 7. 19.
package conditions;

import java.util.Scanner;

public class ConditionsQuiz {

	public static void main(String[] args) {

		// Quiz1
		// 과자의 구입 개수를 입력받아 가격을 출력
		// 과자의 개당 가격은 1000원, 11개 이상 구매시 10% 할인, 100개 이상 구매시 12% 할인된다.
		int price = 0, cnt;
		Scanner sc = new Scanner(System.in);

		System.out.println("과자 개수 입력 : ");
		cnt = sc.nextInt();

		if (cnt < 10)
			price = cnt * 1000;
		else if (cnt <= 99)
			price = (int) (cnt * 1000 * 0.9);
		else
			price = (int) (cnt * 1000 * 0.88);

		System.out.printf("과자 %d개의 총 가격 : %d\n", cnt, price);

		// Quiz2
		// 비행기 탑승시간에 따른 금액을 출력
		// 30분까지의 기본요금 : 30000
		// 이후 10분단위로 5000원의 추가요금이 부과된다.

		int time, price2 = 30000;

		System.out.println("탑승시간 입력 : ");
		time = sc.nextInt();
		if (time > 30)
			price += ((time - 21) / 10) * 5000;
		System.out.println("요금 : " + price2);

		// Quiz3
		// 집, 회사의 주소를 저장, 출력이 가능하도록 코드 작성
		String homeAdress = null, companyAdress = null;
		int sel;
		boolean roof = true;

		while (roof) {
			System.out.printf("1. 우리집 등록\n2. 회사 등록\n3. 등록 보기\n4. 종료\n: ");
			sel = sc.nextInt();
			sc.nextLine();

			switch (sel) {
			case 1:
				System.out.println("집주소 입력 : ");
				homeAdress = sc.nextLine();
				break;

			case 2:
				System.out.println("회사주소 입력 : ");
				companyAdress = sc.nextLine();
				break;

			case 3:
				System.out.printf("집 주소 : %s\n회사 주소 : %s\n", homeAdress, companyAdress);
				break;

			case 4:
				System.out.println("종료합니다.");
				roof = false;
				// System.exit(0); 혹은 return;을 사용하여 종료를 시킬수도 있다.
				break;

			default:
				System.out.println("잘못된 입력입니다. 다시 입력하세요.");
			}

		}

	}

}

'JAVA > 조건식' 카테고리의 다른 글

조건식(switch-case)  (0) 2022.07.18
조건식(else if, else)  (0) 2022.07.18
조건식(if문)  (0) 2022.07.18