'5개의 로또번호 출력하기 - collection실습'에 해당되는 글 1건

  1. 2012.07.28 5개의 로또번호 출력하기 - collection실습

* 5개의 로또번호 출력하기
ex :

package collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeSet;

public class Lotto {

 public static void main(String[] args) {
  //1~45의 숫자중 중복하지 않는 숫자 6개를 만들어 출력하시오.
  //[5,23,1,8,9,14]
  //랜덤한 숫자
  //숫자 - 6개
  //중복되지 않는 숫자를 어떻게 모을 것인가.
  
  ArrayList arrList = new ArrayList();
  //TreeSet lotto=null;

  for(int i =0;i<5;i++){
   TreeSet  lotto = new TreeSet();
   while(true){
    
    lotto.add((int)(Math.random()*45+1));

    if(lotto.size()==6){
  
     break;
    }
   }
  arrList.add(lotto);
  }
  System.out.println("로또 예상 : ");
  System.out.println(arrList);
  
  System.out.println("로또 예상 : ");
  for(int i=0;i<arrList.size();i++){
   System.out.println(arrList.get(i));
  }
  System.out.println("-----------향상된 for문----------");
  for(Object obj : arrList){
   //TreeSet ts = (TreeSet)obj;
   //System.out.println(ts);
   System.out.println(obj.toString());
   
  }

 }

}

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

map실습  (0) 2012.07.28
ArrayList실습  (0) 2012.07.28
컬렉션(collection)  (0) 2012.07.28
인터페이스(interface)  (0) 2012.07.28
추상화(abstract)  (0) 2012.07.28
Posted by 조은성
,