๋ฉ”์†Œ๋“œ ๋ฌธ์ œ ํ’€๊ธฐ3(์‹ฌํ™”)

2023. 1. 29. 22:43ใ†java

public class ๋ฉ”์†Œ๋“œ๋ฌธ์ œ์‹ฌํ™”1_1 {
    public static void main(String[] args) {
        test(5);


    }
    public static void test(int num) {
        for(int i = 1 ; i < 10 ; i++) {
            System.out.println(num + " * " + i + " = " + num * i);
        }
    }
}
public class ๋ฉ”์†Œ๋“œ๋ฌธ์ œ์‹ฌํ™”1_2 {
    public static void main(String[] args) {
        test2(10);

    }
    public static void test2(int num) {
        for (int i = 1 ; i < 101 ; i++) {
            if(i%num==0) {
                System.out.println(i);
            }
        }
    }
}
public class ๋ฉ”์†Œ๋“œ๋ฌธ์ œ์‹ฌํ™”1_3 {
    public static void main(String[] args) {
        int rand = test3();
        System.out.println(rand);
        //์•„๋ž˜ ์œ„ ์ถœ๋ ฅ๋ฌธ ๋™์ผ
        System.out.println(test3());
    }
    public static int test3(){

        return (int)(Math.random() * 50 + 1);    //0 <= x < 51
    }
}
public class ๋ฉ”์†Œ๋“œ๋ฌธ์ œ์‹ฌํ™”1_4 {
    public static void main(String[] args) {
        boolean result = isEven(2);
        if(result) {
            System.out.println("์ง์ˆ˜");
        }
        //์œ„๋ฅผ ์ค„์ด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋”ฉ ๊ฐ€๋Šฅ.
        if(isEven(2)) {
            System.out.println("์ง์ˆ˜");
        }

    }
    //even : ์ง์ˆ˜
    //odd : ํ™€์ˆ˜     ๋ฉ”์†Œ๋“œ๋ช…์ด is๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์ž๋ฃŒํ˜•์ด ํ†ต์ƒ์ ์œผ๋กœ boolean์ด๋‹ค.
    public static boolean isEven(int num) {
        return num % 2 == 0 ? true : false;   //if๋กœ ํ’€์–ด๋„ ๋จ.
    }
}
public class ๋ฉ”์†Œ๋“œ๋ฌธ์ œ์‹ฌํ™”1_5 {
    public static void main(String[] args) {
        //๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•  ๋•Œ ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ!
        int[] arr = {1,2,3,4};
        test(arr);

    }public static void test(int[] arr) {
        for(int i = 0 ; i < arr.length ; i++) {
            System.out.println(arr[i]);

        }
    }
}
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_5_2ํ”„๋ฆฐํŠธX {
    public static void main(String[] args) {
        int[] result = test5_2();
        for (int e : result) {
            System.out.println(e + " ");

        }

    }
    //1,2,3 ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ.
    public static int[] test5_2() {
        int[] arr = {1, 2, 3};
        return arr;
        }
    }
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_5_3ํ”„๋ฆฐํŠธX {
    public static void main(String[] args) {
        int[] a = {2,3,4}; //๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋„ฃ์–ด์ฃผ๊ธฐ!!!
               int[] result = test(a);
               for(int e : result){
                   System.out.println(e + " ");
               }

    }
    //๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด์„ ๋ฐ›์•„, ๊ฐ ์š”์†Œ์˜ ๊ฐ’์— +1ํ•œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ด.
    //1, 2, 3 > 2, 3, 4 ๋ฆฌํ„ด
    public static int[] test(int[] arr) {
        for(int i = 0 ; i < arr.length ; i++) {
            arr[i] = arr[i]+1; //arr[i] += 1;
        }
        return arr;
    }
}
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_6 {
    public static void main(String[] args) {
        int[] a = {5, 10, 100, 200, 150};
        System.out.println(getMaxFromArr(a));

    }
                      //๊ฐ’์„ ๋ฐ›์•„์˜ฌ ๋•Œ ๋ฉ”์†Œ๋“œ๋ช… get ์”€.
    public static int getMaxFromArr(int[] arr) {
        int max = arr[0];
        //๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋น„๊ตํ•ด์„œ ํด ๊ฒฝ์šฐ max์— ๊ณ„์† ์ €์žฅ, ๋‹ค์Œ ์š”์†Œ์™€ ๋น„๊ต
        for(int i = 1 ; i < arr.length ; i++) { //0๋ฒˆ์งธ ์š”์†Œ max์— ์ €์žฅ๋˜์–ด ์žˆ์Œ > i=1
            if(max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }
}
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_7 {
    public static void main(String[] args) {
        String[] a = {"์ž ", "์ด๋ถˆ", "๋ฒ ๊ฐœ"};
        String[] result = a;
        for(String e : result) {
            System.out.println(e + " ");
        }

    }public String[] test7(String[] str){
        for(int i = 0 ; i < str.length ; i++){
            System.out.println(str[i]);
        }
        return str;
    }

}
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_8 {
    public static void main(String[] args) {
        int[] a = {5,6,7,8};
        int[] b = {1,2,3,4};

        for(int e : test8(a,b)) {
            System.out.print(e + " ");
        }
    }
    public static int[] test8(int[] arr1, int[] arr2){
        int[] resultArr = new int[arr1.length + arr2.length];
        for(int i = 0; i < arr1.length ; i++){
            resultArr[i] = arr1[i];
        }
        for(int i = 0; i < arr2.length ; i++){
            resultArr[arr1.length+i] = arr2[i];
        }
        return resultArr;
    }
}
public class ๋ฉ”์†Œ๋“œ์‹ฌํ™”๋ฌธ์ œ1_9 {
    public static void main(String[] args) {
        int[] a = {2,4,5,6,8,10};
        for(int e : test9(a)){
            System.out.print(e + " ");
        }



    }
    public static int[] test9(int[] arr) {
        //๋ฐฐ์—ด์˜ ํฌ๊ธฐ ๊ตฌํ•จ.
        //๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ๋„˜์–ด์˜จ ๋ฐฐ์—ด์—์„œ ์ง์ˆ˜์˜ ์ˆ˜ = ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์˜ ๊ธธ์ด
        int cnt = 0;
        for(int i = 0 ; i < arr.length ; i++) {
            if(arr[i]%2==0) {
                cnt++;
            }
        }
        int[] resultArr = new int[cnt];
        int index = 0; //resultArr๋ฐฐ์—ด์˜ index ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑ.
        for(int i = 0 ; i < arr.length ; i++) {
            if(arr[i]%2==0) {
                resultArr[index] = arr[i];
                index++;  //์œ„ ๋‘ ์ค„์„ ํ•œ ์ค„๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ resultArr[index++] = arr[i];

            }
        }
        return resultArr;

        }
    }

 

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

์ ‘๊ทผ ์ˆ˜์ค€ ์ง€์‹œ์ž (classTest2)  (0) 2023.01.30
ํด๋ž˜์Šค ๋ฌธ์ œ ํ’€๊ธฐ2  (0) 2023.01.29
๋ฉ”์†Œ๋“œ ๋ฌธ์ œ ํ’€๊ธฐ2  (0) 2023.01.29
๋ฐฐ์—ด ๋ฌธ์ œ ํ’€๊ธฐ 2  (0) 2023.01.29
class 2  (0) 2023.01.27