Skip to content

Instantly share code, notes, and snippets.

@ramyo564
Created November 3, 2023 13:18
Show Gist options
  • Save ramyo564/8e75fd8aae399360c827afcf6a08e27e to your computer and use it in GitHub Desktop.
Save ramyo564/8e75fd8aae399360c827afcf6a08e27e to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("[주민등록번 계산]");
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int min = 100000;
int max = 999999;
int random6DigitNumber = random.nextInt(max - min + 1) + min;
System.out.print("출생년도를 입력해 주세요.(yyyy):");
int year = scanner.nextInt();
if (year < 2020) {
System.out.println("출생년도는 2020년도 이상만 가능합니다.");
}else{
System.out.print("출생 월을 입력해 주세요.(mm):");
int month = scanner.nextInt();
if (month > 12 || month < 1){
System.out.println("출생 월은 1월에서 12월까지만 가능합니다.");
}else{
System.out.print("출생 일을 입력해 주세요.(dd):");
int day = scanner.nextInt();
if (day < 1 || day > getDaysInMonth(year, month)){
System.out.println("유효한 일을 입력해주세요");
} else {
scanner.nextLine();
System.out.print("성별을 입력해 주세요.(m/f):");
String gender = scanner.nextLine().toLowerCase().trim();;
if (!gender.equals("m") && !gender.equals("f")) {
System.out.println("유효한 성별을 입력해주세요");
}else{
String finalYear = Integer.toString(year).substring(2);
int finalGender = 0;
if (gender.equals("m")) {
finalGender = 3;
}else {
finalGender = 4;
}
System.out.println(finalYear+month+day+"-"+finalGender+random6DigitNumber);
}
}
}
}
}
public static int getDaysInMonth(int year, int month) {
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 0; // 잘못된 월
}
}
public static boolean isLeapYear(int year) {
// 윤년 판단 로직
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment