IT/자바
HashSet과 TreeSet (로또 프로그램)
Beautifulkim
2018. 5. 14. 11:58
package mymain; import java.util.HashSet; import java.util.Random; import java.util.Set; import java.util.TreeSet; public class MyMain_Set_Test { public static void main(String[] args) { Random rand = new Random(); //Interface(사용메뉴얼) = Class(설계서) //Set<Integer> set_lotto = new HashSet<Integer>(); Set<Integer> set_lotto = new TreeSet<Integer>(); int n = 6; while(n > 0) { int su = rand.nextInt(45) + 1; //1~45 //중복값 허용을 하지 않기 때문에 //동일값이 들어가면 add실패함 if(set_lotto.add(su) == false) continue;; n--; } System.out.println(set_lotto); } }