Recent Posts
Recent Comments
Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- SQL Developer
- Kakao 지도 API
- database
- typing test
- oracle
- 카카오맵
- MySQL
- Oracle 오류
- 데이터베이스 접속
- Jupyter Notebook
- Usecase Diagram
- 자바
- python 기초
- 카카오 오븐
- Set
- 매일 타자연습
- Python
- SQL
- The Network Adapter could not establish the connection
- 10fastfingers.com
- CSV
- Oracle 18c
- oracle 11g
- SEQUENCE
- Kakao Oven
- 카카오맵 API
- Java
- 유스케이스 다이어그램
- tuple
- ORA-1017
Archives
- Today
- Total
Another Brain
mission_java_Lotto645 본문
package com.work.view;
import java.util.Random;
# 배열을 활용하여서 Lotto645 게임을 구현하세요.
임의의 숫자를 추출하는 코드는 아래에 제공합니다.
-- 로또 645는 1~45번 사이의 숫자 6개를 추출하는 게임입니다.
-- 6개의 번호는 중복번호 불가입니다.
-- 1. 로또 1게임 : 1차원 배열 사용
-- 2. 로또 5게임 : 2차원 배열 사용
-- 3. 정렬알고리즘을 활용. 중복되지 않게 출력된 6개의 로또 번호를 올림차순정렬하여 출력.
*/
public class LottoGameTest {
public static void main(String[] args) {
// 임의의 숫자 출력 예시:
for (int index=0; index < 6; index++) {
System.out.print(getLottoNo());
System.out.println("\t");
}
}
/* 객체를 생성하지 않고 임의의 1~45 사이의 숫자를 반환하는 메서드 */
public static int getLottoNo() {
/* 임의의 정수형 숫자를 추출하기 위한 제공 코드입니다. */
Random extractNo = new Random((long)(Math.random() * System.nanoTime()));
int no = extractNo.nextInt(45) + 1; // 0 ~ 44 사이의 임의의 숫자 추출 => +1 => 1 ~ 45 사이 임의숫자 추출
return no;
}
}
package com.work.view;
import java.util.Random;
public class Lotto645 {
public static void main(String[] args) {
int[] lottos = new int[6];
int uniqueLength = 0;
int randomNo;
outerLoop:
while(uniqueLength < 6) {
randomNo = getLottoNo();
for (int index = 0; index < uniqueLength; index++) {
if (lottos[index] == randomNo) {
continue outerLoop;
}
}
lottos[uniqueLength++] = randomNo;
}
System.out.print("[1등 당첨예약 번호] ");
for (int index = 0; index < lottos.length; index++) {
System.out.print(lottos[index] + "\t");
}
System.out.println();
}
public static int getLottoNo() {
Random random = new Random((long)(Math.random() * System.nanoTime()));
return random.nextInt(45) + 1;
}
}
'P > JAVA' 카테고리의 다른 글
4일차_ 클래스(class), 객체(object), 멤버변수, 메서드, 패키지(package), MVC Pattern (0) | 2021.05.25 |
---|---|
3일차(5월 21일)_ 배열(array) (0) | 2021.05.22 |
2일차(5월 18일)_ 자바 프로그래밍 기본, 식별자, 데이터타입, 변수, 연산자, 제어문 (조건문, 반복문) (0) | 2021.05.22 |
mission_java_GuGuDanTest (0) | 2021.05.22 |
1일차(5월 17일)_ JAVA 개발 환경 구축 (0) | 2021.05.22 |
Comments