Last active
January 19, 2025 07:16
-
-
Save Exom9434/64d9d58b12544a1d3f52bca475e16809 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
노재경 | |
import java.text.NumberFormat; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
final int FREE = 0; | |
final int CHILD_DISCOUNT = 4000; | |
final int SPECIAL_DISCOUNT = 8000; | |
final int FULL_PRICE = 10000; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("[입장권 계산]"); | |
int age = getValidatedIntInput(scanner, "나이를 입력해주세요 (숫자): "); | |
int time = getValidatedIntInput(scanner, "입장시간을 입력해주세요 (숫자): "); | |
boolean isHonor = getYesOrNoInput(scanner, "국가유공자 여부를 입력해주세요"); | |
boolean isCard = getYesOrNoInput(scanner, "복지카드 여부를 입력해 주세요"); | |
int entrance; | |
if (age < 3) { | |
entrance = FREE; | |
} else if (time >= 17 || age < 13) { | |
entrance = CHILD_DISCOUNT; | |
} else if (isHonor || isCard) { | |
entrance = SPECIAL_DISCOUNT; | |
} else { | |
entrance = FULL_PRICE; | |
} | |
System.out.println("입장료: " + NumberFormat.getInstance().format(entrance) + "원"); | |
scanner.close(); | |
} | |
private static int getValidatedIntInput(Scanner scanner, String message) { | |
while (true) { | |
try { | |
System.out.print(message); | |
int input = scanner.nextInt(); | |
if (input >= 0) return input; | |
System.out.println("0 이상의 숫자를 입력해주세요."); | |
} catch (Exception e) { | |
System.out.println("유효하지 않은 입력입니다. 숫자를 입력해주세요."); | |
scanner.nextLine(); | |
} | |
} | |
} | |
private static boolean getYesOrNoInput(Scanner scanner, String message) { | |
scanner.nextLine(); | |
while (true) { | |
System.out.print(message + " (y/n): "); | |
String input = scanner.nextLine().trim().toLowerCase(); | |
if (input.equals("y")) return true; | |
if (input.equals("n")) return false; | |
System.out.println("유효하지 않은 입력입니다. y 또는 n을 입력해주세요."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment