Last active
July 7, 2023 17:00
-
-
Save rorrorome/39e6a5400051cd5d8207bb997de47e40 to your computer and use it in GitHub Desktop.
주민번호
This file contains 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
/* | |
* 제로베이스 백엔드 스쿨 15기 | |
* 한새롬 | |
* 미니과제 4. 주민등록번호 생성 프로그램 | |
* | |
* Scanner의 입력함수와 조건문 및 Random클래스를 통한 주민번호 생성 로직 작성 | |
* 1. 주민등록번호 생성 로직에 맞게 주민등록번호 생성 | |
* 2. 입력값은 생년, 월, 일, 성별과 임의의 번호를 통해서 생성 | |
* 3. 임의번호는 Random함수의 nextInt()함수를 통해서 생성 | |
* (임의 번호 범위는 1 ~ 999999사이의 값으로 설정) | |
*/ | |
//Scanner, Random 클래스 | |
import java.util.Scanner; | |
import java.util.Random; | |
public class MiniProject04 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("[주민등록번호 계산]"); | |
System.out.print("출생년도를 입력해 주세요. (yy): "); | |
int year = sc.nextInt(); | |
System.out.print("출생월을 입력해 주세요. (mm): "); | |
int month = sc.nextInt(); | |
System.out.print("출생일을 입력해 주세요. (dd): "); | |
int day = sc.nextInt(); | |
// 조건문 | |
boolean mfChoose = true; | |
int mf = 0; | |
while (mfChoose) { | |
System.out.print("성별을 입력해 주세요. (m/f): "); | |
String gender = sc.next(); | |
if (gender.equals("m") || gender.equals("f")) { | |
if (gender.equals("m")) { | |
mf = 3; | |
} else { | |
mf = 4; | |
} | |
break; | |
} | |
} | |
// Random의 nextInt | |
Random random = new Random(); | |
int rd = random.nextInt(999999) + 1; | |
/* | |
* int rd = random.nextInt(n) 0 <= rd <= n-1 인 랜덤 정수값 반환 | |
* int rd = random.nextInt(n)+1 1 <= rd <= n 인 랜덤 정수값 반환 | |
*/ | |
// 주민번호 출력 | |
System.out.print(String.format("%02d%02d%02d-%d%06d", year, month, day, mf, rd)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment