์‹œํ—˜ ์˜ˆ์ƒ ๋ฌธ์ œ

2023. 2. 2. 17:23ใ†java

1. ๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ๊ธฐ

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

        for(int i = 0; i < lotto.length ; i++){
            lotto[i] = (int)(Math.random() * 45 + 1);
        }
        for(int e : lotto){
            System.out.print(e + " ");
        }
    }
}

1.1 ๋กœ๋˜ ์ค‘๋ณต ์ œ๊ฑฐ

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

        for(int i = 0; i < lotto.length ; i++){
            lotto[i] = (int)(Math.random() * 45 + 1);

            //์ค‘๋ณต ์ œ๊ฑฐ
            for(int j = 0; j < i ; j++){
                if(lotto[i] == lotto[j]){
                    i--;
                    break;
                }
            }
        }
        for(int e : lotto){
            System.out.print(e + " ");
        }
    }
}

 

2. ์„ธ ์ˆ˜ ์Šค์บ๋„ˆ๋กœ ์ž…๋ ฅ๋ฐ›์•„  ํฐ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ด (if๋ฌธ ๋น„๊ต ์กฐ๊ฑด 3๊ฐ€์ง€ ๋“ค์–ด๊ฐ€์•ผ ํ•จ)

import java.util.Scanner;

public class ๋ฌธ์ œ09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a, b, c;

        System.out.println("-์ถœ๋ ฅ์˜ˆ์‹œ-");
        System.out.print("์ฒซ๋ฒˆ์งธ ์ˆ˜ : ");
        a = sc.nextInt();
        System.out.print("๋‘๋ฒˆ์งธ ์ˆ˜ : ");
        b = sc.nextInt();
        System.out.print("์„ธ๋ฒˆ์งธ ์ˆ˜ : ");
        c = sc.nextInt();

        if(a > b && a > c && b > c) {
            System.out.println(a + " > " + b + " > " + c);
        } else if(a > b && a > c && b < c) {
            System.out.println(a + " > " + c + " > " + b);

        }if(b > a && b > c && a > c) {
            System.out.println(b + " > " + a + " > " + c);
        } else if(b > a && b > c && c > a) {
            System.out.println(b + " > " + c + " > " + a);
        }

        if(c > a && c > b && a > b) {
            System.out.println(c + " > " + a + " > " + b);
        } else if(c > a && c > b && b > a) {
            System.out.println(c + " > " + b + " > " + a);
        }
    }
}

3. ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ€๊ฐ’ ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ

public class MaxMin {
    public static void main(String[] args) {
        int[] array = new int[10];
        for(int i = 0; i < array.length ; i++){
            array[i] = (int)(Math.random() * 100 + 1);
        }
        //๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋ชจ๋“  ์ •์ˆ˜ ์ถœ๋ ฅ
        for (int e : array){
            System.out.print(e + " ");
        }
        System.out.println();
        //๋ฐฐ์—ด์— ์ €์žฅ๋œ ์ˆ˜ ์ค‘ ์ตœ์†Œ๊ฐ’
        int min = array[0];
        for(int i = 0; i < array.length ; i++){
            if(array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("์ตœ์†Œ๊ฐ’ : " + min);

        //๋ฐฐ์—ด์— ์ €์žฅ๋œ ์ˆ˜ ์ค‘ ์ตœ๋Œ€๊ฐ’
        int max = array[0];
        for(int i = 0; i < array.length ; i++){
            if(array[i] > max){
                max = array[i];
            }
        }
        System.out.println("์ตœ๋Œ€๊ฐ’ : " + max);
        
    }
}

3.1 

์ตœ๋Œ€๊ฐ’ ์ตœ์†Œ๊ฐ’ ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค์–ด ๋ฆฌํ„ด

public class Max {
    public static void main(String[] args) {
        int[] arr = {10, 150, 50, 250, 70};
        System.out.println(getMaxFromArr(arr));
        System.out.println(getMinFromArr(arr));


    }

    public static int getMaxFromArr(int[] arr){
        int max = arr[0];
        for(int i = 0; i < arr.length ; i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }

    public static int getMinFromArr(int[] arr){
        int min = arr[0];
        for(int i = 0; i < arr.length; i++){
            if(arr[i] < min){
                min = arr[i];

            }
        }
        return min;
    }
}

 

4.ํด๋ž˜์Šค๋กœ ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ณ  ์‹คํ–‰.

๋ฉ”์†Œ๋“œ ๋งŒ๋“ค ๋•Œ  static ๋ถ™์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” main ๋ฉ”์†Œ๋“œ ๋„ฃ์€ ํด๋ž˜์Šค์—์„œ ๋ฉ”์†Œ๋“œ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋งŒ  static ์ ์–ด์•ผ ํ•จ.

๋ฉ”์ธ ๋ฉ”์†Œ๋“œ์—  static์ด ์ ํ˜€ ์žˆ์–ด์„œ ๋™์ผํ•˜๊ฒŒ ๋งž์ถ”๋Š” ๊ฒƒ!

์ฐ ์„ค๊ณ„๋„ ํด๋ž˜์Šค ๋งŒ๋“ค ๋•Œ๋Š”  public  void ๋ฉ”์†Œ๋“œ๋ช…๋งŒ ์ ์–ด๋„ ๋จ.

 

** ์ค‘๋ณต ๊ฐ’ ์—†์• ๋Š” ์ฝ”๋“œ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ ํ’€๊ธฐ ์กฐ๊ธˆ ๊ดœ์ฐฎ์Œ.

'java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

wrapper class  (0) 2023.02.03
โ˜…์˜ˆ์™ธ์ฒ˜๋ฆฌ  (0) 2023.02.03
์ธํ„ฐํŽ˜์ด์Šค ๋ฌธ์ œ ํ’€๊ธฐ  (0) 2023.02.02
Interface(์ธํ„ฐํŽ˜์ด์Šค)  (0) 2023.02.02
String ํด๋ž˜์Šค  (0) 2023.02.02