로또번호 6개 다른값뽑기
by #독개#package hacker; import java.time.LocalDate; import java.util.ArrayList; import java.util.Random; public class Main { public static boolean search(int start , ArrayList<Integer> lotto){
int size = lotto.size();
for(int ct= 0; ct<size; ct++) {
if(start == lotto.get(ct)) {
return true; } } return false; }
public static void main(String[] args) {
System.out.print(LocalDate.now().toString() + " 로또번호 : "); Random rand = new Random(); int value = 0;
ArrayList<Integer> lotto = new ArrayList<Integer>();
//로또번호 6개추출 for(int ct = 0 ; ct<6;ct++) { value = rand.nextInt(45) + 1; while(Main.search(value, lotto)) { value=rand.nextInt(45) + 1; } lotto.add(value);
}
for(int a : lotto) { System.out.print(a + " "); }
} }
|
내꺼 package hacker; import java.time.LocalDate; import java.util.ArrayList; import java.util.Random; public class Main { public static void main(String[] args) {
Random random = new Random();
int value ; boolean a = true ; ArrayList<Integer> list = new ArrayList<Integer>() ;
value = (random.nextInt(45)) + 1 ; list.add(value);
for(int ct=0 ; ct < 5; ct++) { value = (random.nextInt(45)) + 1 ;
for(int ct2=0 ; ct2<list.size() ; ct2++) { a=false; if (list.get(ct2) == value) { ct--; a=true; break; }
}
if(!a) {
list.add(value); }
}
for(int c : list) { System.out.print(c + " "); }
}
} |
'🔥 프로그래밍 학습 > JAVA' 카테고리의 다른 글
추상화 클래스 , 인터페이스 (0) | 2020.05.25 |
---|---|
자바 분석할것2 (0) | 2020.05.25 |
JAVA 클래스와 메소드2 (상속) (0) | 2020.05.24 |
클래스,생성자,인스턴스맴버,정적맴버,final,상수 (0) | 2020.05.21 |
JAVA 기초 (0) | 2020.05.21 |
블로그의 정보
독한 개발자
#독개#