본문 바로가기

프로그래밍/자바

[자바] 정올 170 ~ 174 : 함수1 - 형성평가1 ~ 5

728x90
반응형
SMALL

형성평가1

public class Main {
    public static void main(String[] args) {
        System.out.println("first");
        pr();
        System.out.println("second");
        pr();
        System.out.println("third");
        pr();
    }
    public static void pr(){
        System.out.println("@@@@@@@@@@");
    }

}

형성평가2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a =sc.nextInt();
        int answer = addAll(a);
        System.out.println(answer);
    }
    public static int addAll(int n){
        int a = 0;
        for(int i = 0; i <= n; i++) a+=i;
        return a;
    }
}

형성평가3

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        pr(a);
    }

    public static void pr(int a) {

        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= a; j++) {
                System.out.print((i*j) + " ");
            }
            System.out.println();
        }
    }
}

형성평가4

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt();
        int answer = an(a,b);
        System.out.println(answer);
    }
    public static int an(int a, int b){
        int max = a > b ? a : b;
        int min = a < b ? a : b;
        return (int)(Math.pow(max, 2) - Math.pow(min, 2));
    }
}

형성평가5

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] arr = new int[3][3];
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                arr[i][j] = sc.nextInt();
            }
        }
        pr_answer(arr);
    }//end main
    public static void pr_answer(int[][] arr){
        int sum = 0, total = 0;
        int[] col = new int[3];
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                System.out.print(arr[i][j] + " ");
                col[j] += arr[i][j];
                sum +=  arr[i][j];
            }
            total += sum;
            System.out.println(sum);
            sum = 0;
        }

        for(int i = 0; i < col.length; i++){
            System.out.print(col[i] + " " );
        }
        System.out.println(total);

    }
}
728x90
반응형
LIST