본문 바로가기

프로그래밍/자바

[자바] 정올 571 ~ 578 : 함수1 - 자가진단1 ~ 8

728x90
반응형
SMALL

자가진단1

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

        System.out.println("~!@#$^&*()_+|\n".repeat(n));
    }

}

자가진단2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        won(n);
    }
    public static void won(int n){
        double a = n*n*3.14;
        System.out.printf("%.2f",a);
    }

}

자가진단3

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

자가진단4

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr = sc.nextLine().trim().split("\\s+");
        int[] arr_int = new int[3];
        for(int i = 0; i < 3; i++) arr_int[i] = Integer.parseInt(arr[i]);
        findMax(arr_int);
    }

    public static void findMax(int[] arr){
        Arrays.sort(arr);
        System.out.println(arr[arr.length-1]);
    }
}

자가진단5

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();
        power_two(a,b);
    }
    public static void power_two(int a, int b){
        System.out.printf("%d",(int)Math.pow(a,b));
    }

}

자가진단6

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().trim();
        cal(str);
    }
    public static void cal(String str){
        String[] arr  = str.split("\\s+");
        int answer;
        if(arr[1].equals("+")) {
            answer = Integer.parseInt(arr[0]) + Integer.parseInt(arr[2]);
        }
        else if(arr[1].equals("-")){
            answer = Integer.parseInt(arr[0]) - Integer.parseInt(arr[2]);
        }
        else if(arr[1].equals("/")) {
            answer = Integer.parseInt(arr[0]) / Integer.parseInt(arr[2]);
        }
        else if(arr[1].equals("*")) {
            answer = Integer.parseInt(arr[0]) * Integer.parseInt(arr[2]);
        }else answer = 0;
        System.out.println(str + " = " +answer);
    }
}

자가진단7

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 max =find_max(a, b);
        int min = find_min(a, b);
        if(a == max)  System.out.printf("%d %d", max/2, min*2);
        else System.out.printf("%d %d",  min*2,max/2);
    }
    public static int find_max(int a, int b){
        return a > b ? a : b;
    }
    public static int find_min(int a, int b){
        return  a < b ? a : b;
    }

}

자가진단8

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();
        if(a < b) GUGUDAN(a, b);
        else GUGUDAN(b, a);
    }
    public static void GUGUDAN(int a, int b){
        for(int i = a; i <=b; i++){
            System.out.println("== "+i+"dan ==");
            for(int j = 1; j < 10; j++){
                System.out.printf("%d *%2d =%3d",i,j,i*j);
                System.out.println();
            }
            System.out.println();
        }
    }

}
728x90
반응형
LIST