java 로 Lotto 만들어 보기

import java.util.Scanner;

public class Ex_04_25_Math_lotto_남동길 {

public static void main(String[] args) {
int[] lotto = new int[6]; // 1~45 중복 안되게 삽입
     int[] my = new int[6]; // Scanner 입력
     int count=0; // 맞은 갯수
     Scanner sc = new Scanner(System.in);
     int i,j;
     
     for(i=0; i<lotto.length; i++){
        lotto[i] = (int)((Math.random()*45)+1);
        if(i>0){
           for(j=0; j<i; j++){
              while(lotto[i]==lotto[j]){
                 lotto[i] = (int)((Math.random()*45)+1);
              }
           }
        }
     }
        
     
     
     
     for(i=0; i<my.length; i++){
        System.out.print("숫자"+(i+1)+" : ");
        my[i] = sc.nextInt();         
     }
     System.out.println();
     System.out.println("내가 뽑은 숫자 : ");
     for(i=0; i<my.length; i++){
        System.out.print(my[i]+" ");
     }
     System.out.println();
     System.out.println();
     System.out.println("컴이 뽑은 숫자 : ");

     for(i=0; i<lotto.length; i++){
        System.out.print(lotto[i]+" ");
     }
     System.out.println();
     
     System.out.println();
     
     for(i=0; i<my.length; i++){
        for(j=0; j<lotto.length; j++){
           if(my[i] == lotto[j]){
              count += 1;
           }
        }
     }
     
     System.out.println("맞은 갯수 : "+count);

switch(count){
case 6: System.out.println("1등 당첨입니다.");
break;
case 5: System.out.println("2등 당첨입니다.");
break;
case 4:System.out.println("3등 당첨입니다.");
break;
case 3:System.out.println("4등 당첨입니다.");
break;
default : System.out.println("꽝~!~!~!");
}

}

}



                         결과
                         숫자 6개를 입력받아 
                         무작위로 중복되지 않게 생성한 숫자 6개와 비교하여
                         같은개수에 따라서 등수와 꽝을 출력해주는 프로그램

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

About G1 GC

sneak peek jitpack

About idempotent

C 언어 구조체의 포인터 멤버 변수

Synology NAS에 MariaDB 10에 Mysql workbench로 원격접속하기

About Websocket minimize data size and data transfer cost on cloud