2013년 1월 15일 화요일
[Java] 로또생성 및 맞추기(How many times can you Won the Lottery with 6 random number)
public class Lotto {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Random r = new Random();
int correct = 0;
int numOftry = 1;
System.out.println("이번주 로또 입니다.");
while(true){
int num = r.nextInt(45)+1;
if(list.contains(num)) continue;
else
list.add(num);
if(list.size()==6) break;
}//이 부분이 참 유용하드라
for(int i=0; i<list.size(); i++){
System.out.print(list.get(i) + ", ");
}
System.out.println();
Iterator<Integer> iter = list.iterator();
ArrayList<Integer> list2 = new ArrayList<Integer>();
while(true){
correct=0;
System.out.println(numOftry + "번째 시도");
while(true){
int num2 = r.nextInt(45)+1;
if(list2.contains(num2)) continue;
else
list2.add(num2);
if(list2.size()==6) break;
}
for(int i=0; i<list2.size(); i++){
if(list.contains(list2.get(i))){
correct++;
}
}
if(correct==6) break;
for( ; list2.size()!=0; ){
list2.remove(0);
}
numOftry++;
}
while(iter.hasNext()){
System.out.print(iter.next() + ", ");
}//이렇게 Iterator을 이용하여 자주 사용한다. 유념할 것
System.out.println();
}
}
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기