Skip to content

Instantly share code, notes, and snippets.

@Exom9434
Last active May 25, 2024 06:33
Show Gist options
  • Save Exom9434/5f038f305071ee4accfa9cc816715302 to your computer and use it in GitHub Desktop.
Save Exom9434/5f038f305071ee4accfa9cc816715302 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]);
//앞쪽 6자리용
String yearString = Integer.toString(year);
String firstTwo = yearString.substring(yearString.length() - 2); //주민등록 번호 앞 두자리
String monthString = String.format("%02d", month);
String dayString = String.format("%02d", day);
//뒷쪽 7자리용
String genderInt;
if (gender.equals("m")) { //3항연산자를 통한 if문 간소화
genderInt = (year < 2000) ? "1" : "3";
} else {
genderInt = (year < 2000) ? "2" : "4";
}
Random random = new Random();
int randomNum = random.nextInt(999999) + 1;
String randomNumString = String.format("%06d", randomNum);
String socialNum = firstTwo + monthString + dayString + "-" + genderInt + 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