본문 바로가기

프로그래밍/자바(JAVA)

[자바/JAVA] 자바 로또 소스 코드

자바 로또 소스 코드


import java.io.*;

class Exam01{
 public static void main(String args[]) throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

  int[] num = new int[6];
  int[] lottoNum = new int[6];
  
  for(int i = 0; i <lottoNum.length; i++){
   lottoNum[i] = (int)(Math.random()*45+1);
   for(int j = 0; j < i ; j++){
    if(lottoNum[i] == lottoNum[j]){
     i--;
     break;
    }
   }
  }  

  for(int i = 0; i < num.length; i++){
   System.out.print("선택번호 입력 : ");
   num[i] = Integer.parseInt(in.readLine());
   do{
    if(num[i]>45 || num[i] <= 0){
    System.out.println("잘못입력 했습니다.");
    System.out.print("선택번호 입력 : ");
    num[i] = Integer.parseInt(in.readLine());
    }
   }while(num[i]>45 || num[i] <= 0);
   for(int j = 0; j<i ; j++){
    if(num[i] == num[j]){
     System.out.println("중복된 숫자입니다.");
     i--;     
     break;
    }
   }
  }
  System.out.print("선택번호 : ");
  for(int i = 0; i <num.length; i++){
   System.out.print(num[i]);
   if(i<num.length-1){
    System.out.print(",");
   }
  }
  System.out.println();
  System.out.print("추첨번호 : ");
  for(int i = 0; i <lottoNum.length; i++){
   System.out.print(lottoNum[i]);
   if(i<lottoNum.length-1){
    System.out.print(",");
   }
  }
  System.out.println();
  System.out.print("당첨번호 : ");
  for(int i = 0; i < 6; i++){
   for(int j = 0; j<i; j++){
    if(num[i] == lottoNum[j]){
     System.out.print(lottoNum[j]);
     if(i<num.length-1){
      System.out.print(",");
      }
    }
   }  
  }
 }
}