(Q) 다음 랜덤값을 구하여 화면에 출력하세요
1) 주사위 1개를 던진 결과
2) 로또 번호 1개를 추첨한 결과 (1~45)
3) OTP번호 1개를 생성한 결과 (6자리 정수)
4) 동전을 던졌을 때 예사되는 결과 앞 또는 뒤
package day06;
import java.util.Random;
public class Day06_random_Test02 {
public static void main(String[] args) {
Random r = new Random();
// 1. 주사위
int dice = r.nextInt(6) + 1;
System.out.println(dice);
// 2. 로또 번호 1개 추첨 (1~45)
int lotto = r.nextInt(45) + 1;
System.out.println(lotto);
// 3-1. OTP 번호 생성 (6자리 정수 - 000000 ~ 999999)
int otpZero = r.nextInt(1000000);
System.out.println(otpZero);
// 3-2. OTP 번호 생성 (6자리 정수 - 100000 ~ 999999)
int otp = r.nextInt(900000) + 100000;
System.out.println(otp);
// 4. 동전 1개를 던졌을 때 결과
boolean coin = r.nextBoolean();
if(coin) {
System.out.println("앞");
}
else {
System.out.println("뒤");
}
}
}
** OTP 번호 문제 (100000 ~ 999999)
- 최소값을 100000으로 해야 하므로 숫자2에 100000 대입
- 최대값을 999999으로 해야 하므로 숫자1에 900000 대입 (0 ~ 899999)
- 최종 결과 : 0 + 100000 < OTP < 899999 + 100000
'국비교육 > 국비교육 복습' 카테고리의 다른 글
Day06_random_Test04 : 랜덤 구구단 문제 (3번 틀리면 게임 오버) (0) | 2022.08.07 |
---|---|
Day06_random_Test03 : 랜덤 구구단 문제 출력 (random 활용) (0) | 2022.08.07 |
Day06_random_Test01 : 난수(random)의 생성 범위 제한(Math.random()와 random 라이브러리) (0) | 2022.08.07 |
Day06_loop3_Test03 : 숫자 모래성 게임 (while 반복문 활용 - 무한 반복문) (★) (0) | 2022.08.07 |
Day06_loop2_Test01 : 초 단위로 1시간 출력 (중첩 반복문) (★) (0) | 2022.08.07 |