Skip to content

Instantly share code, notes, and snippets.

@Exom9434
Last active August 23, 2024 00:20
Show Gist options
  • Save Exom9434/3c5ba2bac87c482e904084af0ba87e88 to your computer and use it in GitHub Desktop.
Save Exom9434/3c5ba2bac87c482e904084af0ba87e88 to your computer and use it in GitHub Desktop.
// 노재경
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("[주민등록번호 생성]");
System.out.print("출생년월일을 입력해주세요 (yyyy.mm.dd): ");
String birthDate = scanner.nextLine();
System.out.print("성별을 입력해주세요 (m/f): ");
String gender = scanner.nextLine();
// 년월일 분리
String[] dateParts = birthDate.split("\\.");
int year = Integer.parseInt(dateParts[0]);
int month = Integer.parseInt(dateParts[1]);
int day = Integer.parseInt(dateParts[2]);
String firstPart = String.format("%02d%02d%02d", year % 100, month, day);
String genderDigit = (gender.equalsIgnoreCase("m")) ? "2" : "4";
// 뒤쪽 6자리 랜덤 생성
String randomNumString = String.format("%06d", new Random().nextInt(999999) + 1);
// 생성결과물 결합
String socialNum = firstPart + "-" + genderDigit + randomNumString;
scanner.close();
System.out.println(socialNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment