[자바] 26일차 - 자바 기초 정리(해쉬셋HashSet, 로또번호 생성/비교 프로그램) :: 소림사의 홍반장!

< GameLotto.java >

 

package lotto;

 

public class GameLotto {

 

        public static void main(String[] args) {

              

               new GameLotto().execute();

              

        }

 

        public void execute() {

               ComLotto c = new ComLotto();

               UserLotto u = new UserLotto();

              

               c.execute();

               System.out.println();

               u.execute();

              

               int[] comLotto = c.comLotto;

               int[] userLotto = u.userLotto;

              

               // 3개 맞으면 4(5000), 4개 맞으면 3(5만원),

               // 5개 맞으면 2(200만원), 6개 맞으면 1(12)

              

               int count=0;

               for(int i=0; i<comLotto.length; i++) {

                      

                       for(int j=0; j<userLotto.length; j++) {

                             

                              if(comLotto[i]==userLotto[j])

                                      count++;

                       }

               }//end i

              

               String msg = "";           

               switch(count) {

 

                       case 3:

                              msg = "4등입니다! 5000원 당첨!";

                              break;

                       case 4:

                              msg = "3등입니다! 5만원 당첨!";

                              break;

                       case 5:

                              msg = "2등입니다! 200만원 당첨!";

                              break;

                       case 6:

                              msg = "1등입니다! 12억원 당첨!";

                              break;

                       default:

                              msg = "!!!";      

                                                    

               }//end switch

               System.out.println(msg);

              

        }//end execute

 

}

 

 

 

< ComLotto.java >

 

package lotto;

 

import java.util.*;

 

// 컴퓨터가 6개의 숫자 뽑아내기

public class ComLotto {

 

        int[] comLotto = new int[6];

       

        public static void main(String[] args) {

 

               new ComLotto().execute();

        }

 

        public void execute() {

       

               outer:for(int i=0; i<6; i++) {

                       int com = (int)(Math.random()*45)+1;

                       for(int j=0; j<i; j++) {

                              if(comLotto[j] == com) {

                                      i--;

                                      System.out.println("중복 : "+com);

                                      continue outer;

                              }

                       }

                       comLotto[i] = com;

                       System.out.println(com);

               }

       

               Arrays.sort(comLotto);

               for(int com : comLotto)

                       System.out.printf("%2d  ", com);

        }//end execute()

}

 

 

 

< UserLotto.java >

 

package lotto;

 

import java.util.*;

 

public class UserLotto {

       

        int[] userLotto = new int[6];

 

        public static void main(String[] args) {

               new UserLotto().execute();

        }

 

        public void execute() {

               Scanner scan = new Scanner(System.in);

                

               for (int i = 0; i < 6; i++) {

                       try {

                              System.out.print("Lotto "+(i+1)+"번째 > ");

                              String userStr = scan.next();

                              int user = Integer.parseInt(userStr);

                              if(user < 1 || user > 45) {

                                      throw new Exception();

                              }

//                            System.out.println(user);

                             

                              // 중복검사 : 중복이 있을 경우

                              for(int j=0; j<i; j++) {

                                      if(userLotto[j] == user) {

                                             i--;

                                             throw new Exception("중복된 숫자가 있습니다.");

                                      }

                              }

                             

                              userLotto[i] = user;

                                                            

                       } catch (Exception e) {

                              i--;

                              String msg = e.getMessage();

                              if(msg==null|| e instanceof NumberFormatException)

                                      System.out.println("1~45 숫자만 넣어라!");

                              else

                                      System.out.println(msg);

                       }finally {

                              for(int j=0; j<i+1; j++)

                                      System.out.printf("%2d  ", userLotto[j]);

                              System.out.println();

                       }

               }

        }

}

 

해쉬셋 (HashSet)

- 순서가 없다. 중복이 없다.

 

2012/09/01 - [Dev. 자바/API 및 이론] - [JAVA API] java.util.HashSet

 

2012/09/02 - [Dev. 자바/참고소스] - [자바 소스] HashSet 사용시 중복 여부 판별 부여 방법

 

 

다른 카테고리의 글 목록

Dev. 640시간 뭉개기/강의내용정리 카테고리의 포스트를 톺아봅니다