KIC/JAVA

day16 - JAVA (자바 예제)

바차 2021. 7. 14. 17:36
반응형

[로또 뽑기 예제]

package javaPro.java_array;

import java.util.Arrays;

public class ArrayEx31 {
   public static void main(String[] args) {
       int[] balls = new int [45];
       int[] lotto = new int [6];

       for(int i = 0; i < balls.length; i++){
           balls[i] = i+1;
       }


       for(int i = 0; i < 1000; i++){
           int f = (int)(Math.random()*45); // 0부터 44 까지 랜덤 수를 주기 때문에  사실상 인덱스를 뜻함
           int t = (int)(Math.random()*45);

           int tmp = balls[t];  // 값을 랜덤으로 바꾸는게 아니라 인덱스를 랜덤으로 바꾸는 것, 인덱스는 중복으로 떠도 상관 없으니까
           balls[t] = balls[f];
           balls[f] = tmp;
           
        }
        
        for(int i = 0; i< lotto.length; i++){
            lotto[i] = balls[i];
        }
        

        for(int b : lotto){
           System.out.print(b + " ");
        }
        
        System.out.println();
       
        //sort 알고리즘
       for(int i = 0; i< lotto.length; i++){
           boolean change = false;
           for(int j = 0; j< lotto.length - 1 - i; j++){
               if(lotto[j]>lotto[j+1]){
                   int tmp = lotto[j];
                   lotto[j] = lotto[j+1];
                   lotto[j+1] = tmp;
                   change = true;
                } 
            }
            if (!change) break;
        }

        //내장 함수
        //Arrays.sort(lotto);
        /*
        for(int b : lotto){
           System.out.print(b + " ");
        }
        */


        for(int b : lotto){
           System.out.print(b + " ");
        }
    }
}

 

 

 

 

[숫자 맞추기 예제]

package javaPro.java_array;

import java.util.Scanner;


public class arrayEx01 {

/*
 * 숫자 맞추기 게임
 * 1)  시스템이 4자리의 서로 다른 수를 저장한 후
 * 2)  사용자가 저장된 수를 맞추는 게임
 *     자리수도 맞는 경우 : 스트라익
 *     자리수는 틀리지만 숫자가 존재하면 : 볼
 *     4스트라익이 되면 정답
 */

   public static void main(String[] args) {
       
       int[] num = new int [10];
       int[] answer = new int [4];

       for(int i = 0; i < num.length; i++){
           num[i] = i;
       }


       for(int i = 0; i < 1000; i++){
           int f = (int)(Math.random()*10); //사실상 인덱스를 뜻함
           int t = (int)(Math.random()*10);

           int tmp = num[t];  // 값을 랜덤으로 바꾸는게 아니라 인덱스를 랜덤으로 바꾸는 것, 인덱스는 중복으로 떠도 상관 없으니까
           num[t] = num[f];
           num[f] = tmp;
           
        }
        
        for(int i = 0; i< answer.length; i++){
            answer[i] = num[i];
        }
        

        for(int b : answer){
           System.out.print(b + " ");
        }

        System.out.println();

        //사용자로부터 정답이 입력될 때까지 입력 받음
        Scanner scan = new Scanner(System.in);
        int data[] = new int[4];

        while(true){
            System.out.print("서로 다른 4자리의 숫자 입력하세요 :");
            String input = scan.next();

            for (int i = 0; i < data.length; i++) {
                data[i] = input.charAt(i) - '0';// 문자로 먼저 입력 받아서 charAt 함수를 써서 charAT(i) -> 해당 문자열의 i번째 문자
                // -'0'은 아스키 49
                

            }

                int strike = 0;
                int ball = 0;

                for (int i = 0; i < data.length; i++) {
                    for (int j = 0; j < answer.length; j++) {
                        if(data[i] == answer[j]){
                            if(i==j) strike++;
                            else ball++;
                        }
                        
                    }
                }
                System.out.println("ball: " + ball);
                System.out.println("strike: " + strike);

                if(strike == 4){
                    System.out.println("정답");
                    break;
                }


        }
   } 
}

 

 

[총점 평균 구하기 예제]

package javaPro.java_array;

// 과목에 대한 총점 평균 구하기

public class arrayEx10 {
    public static void main(String[] args) {
        int score[][] = {
            {100, 80, 90}, {80, 95, 100}, {60, 65, 70}, {85, 70, 75}, {90, 90, 80}
        };

        System.out.println("\t 국어\t 영어\t 수학\t 총점\t 평균\t ");

        int total[] = new int[3];
        for (int i = 0; i < score.length; i++) {
            System.out.print((i+1) + "번 학생 : ");
            int sum = 0;
            for (int j = 0; j < score[i].length; j++) {
                System.out.print(score[i][j] + "\t");
                sum += score[i][j];
                total[j] += score[i][j];
            }
            //System.out.print(sum + "\t" + (double)sum/score[i].length);

            System.out.printf("%d \t %.2f \n", sum, (double)sum/score[i].length);
        }

        System.out.print("과목 총점 : ");
        for (int j2 = 0; j2 < total.length; j2++) {
            System.out.print(total[j2]+ "\t");
        }
        System.out.println();
        
        System.out.print("과목 평균 : ");
        for (int i = 0; i < total.length; i++) {
            System.out.printf("%.2f \t", (double) total[i]/score.length);
        }

    }
    
}

결과

 

300x250