연습 문제

 

풀이
public class LottoMain {
 public static void main(String[] args) {
  LottoMenu menu = new LottoMenu();
  menu.display();
 }
}

 


import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class LottoManage {
 public void createNo(List<Integer> randomList) {
  randomList.clear();
  
  Random rd = new Random();
  
  int tempInt;
  boolean randomFlag;
  
  while (randomList.size() < 7) {
   tempInt = rd.nextInt(45) + 1;
   randomFlag = false;
   for (Integer i : randomList) {
    if (i == tempInt) {
     randomFlag = true;
     break;
    }
   }
   if (!randomFlag) {
    randomList.add(tempInt);
   }
  }
  System.out.print("당첨번호 : ");
  for (int i = 0; i < 6; i++) {
   System.out.print(randomList.get(i) + " ");
  }
  System.out.println("\n보너스번호 : " + randomList.get(6));
 }
 
 public void inputNo(List<Integer> randomList, List<Integer> inputList, Scanner sc) {
  inputList.clear();
  
  int no;
  boolean dupFlag;
  
  for (int i = 0; i < 6; i++) {
   while (i == inputList.size()) {
    System.out.print((i + 1) + "번째 숫자를 입력하세요: ");
    no = sc.nextInt();
    if (no < 1 || no > 45) {
     System.out.println("숫자는 1~45 사이의 숫자만 입력가능합니다.");
     continue;
    }
    dupFlag = false;
    for (Integer j : inputList) {
     if (j == no) {
      System.out.println("중복된 숫자는 입력할 수 없습니다.");
      dupFlag = true;
      break;
     }
    }
    if (!dupFlag) {
     inputList.add(no);
    }
   }
  }
  drawNo(randomList, inputList);
 }
 
 private void drawNo(List<Integer> randomList, List<Integer> inputList) {
  int count = 0;
  boolean bonusFlag = false;
  
  for (int i = 0; i < 6; i++) {
   for (Integer j : inputList) {
    if (randomList.get(i) == j) {
     count++;
    }
   }
  }
  
  System.out.print("결과: ");
  switch (count) {
   case 3 :
    System.out.println("5등");
    break;
   case 4 :
    System.out.println("4등");
    break;
   case 5 :
    for (Integer i : inputList) {
     if (i == randomList.get(6)) {
      System.out.println("2등");
      bonusFlag = true;
      break;
     }
    }
    if (!bonusFlag) {
     System.out.println("3등");
    }    
    break;
   case 6 :
    System.out.println("1등");
    break;
   default :
    System.out.println("꽝");
    break;
  }
 }
}

 import java.util.ArrayList;
  import java.util.List;
  import java.util.Scanner;

  public class LottoMenu {
   public void display() {
    LottoManage lm  = new LottoManage();
    Scanner sc   = new Scanner(System.in);
    
    List<Integer> randomList  = new ArrayList<>();
    List<Integer> inputList  = new ArrayList<>();
    
    int selectedNo  = 0;
    boolean mainFlag  = false;
    
    do {
     System.out.print("메뉴를 선택하세요. (1. 난수생성 / 2. 번호입력 / 3. 종료) : ");
     selectedNo = sc.nextInt();
     
     switch(selectedNo) {
      case 1:
       lm.createNo(randomList);
       break;
      case 2:
       if (randomList.size() == 0) {
        System.out.println("난수를 먼저 생성해야 합니다.");
       } else {
        lm.inputNo(randomList, inputList, sc);
       }
       break;
      case 3:
       System.out.println("프로그램을 종료합니다.");
       mainFlag = true;
       break;
      default:
       System.out.println("존재하지 않는 메뉴입니다.");
       break;
     }
    } while(!mainFlag);
    sc.close();
   }
  }

 

 

결과

'JAVA 기초 정리 > JAVA' 카테고리의 다른 글

5. 연습문제 : 평균성적순위계산기  (0) 2019.12.19
2. 연습문제 : 만년달력  (0) 2019.12.19
4. 연습문제 : 경우의 수  (0) 2019.12.19
3. 연습문제 : 정삼각형  (0) 2019.12.19
6. 연습문제 : 구구단찍기  (0) 2019.12.19

+ Recent posts