쉽게 배우는 자바 프로그래밍

[쉽게 배우는 자바 프로그래밍] Chapter03_프로그래밍 문제

seungh22 2024. 3. 9. 00:38

Q) 01_키보드로 입력한 정수가 19 이상이면 '성년', 아니면 '미성년'을 춮력하는 프로그램을 if~else 문을 사용해 작성하라.

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("정수를 입력하세요 : ");
        int x = in.nextInt();

        if ( x >= 19 )
            System.out.println("성년");
        else
            System.out.println("미성년");
    }
}

 

Q 02_ 키보드로 등수를 입력받아 1등이면 '아주 잘했습니다', 2~3등이면 '잘했습니다', 4~6등이면 '보통입니다', 그 외 등수이면 '노력해야겠습니다' 라고 출력하는 프로그램을 switch문을 사용해 작성하라.

import java.util.Scanner;

public class Main {
    public static  void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("등수를 입력하세요 : ");
        int x = in.nextInt();

        switch (x) {
            case 1:
                System.out.print("아주 잘했습니다.");
                break;
            case 2:
                System.out.print("잘했습니다.");
                break;
            case 3:
                System.out.print("잘했습니다.");
                break;
            case 4:
                System.out.print("보통입니다.");
                break;
            case 5:
                System.out.print("보통입니다.");
                break;
            case 6:
                System.out.print("보통입니다.");
                break;
            default:
                System.out.print("노력해야겠습니다.");
        }
    }
}

 

Q) 03_키보드로 입력된 양의 정수 중에서 짝수만 덧셈해서 출력하는 코드를 do~while 문을 사용해 작성하라. 단, 입력된 정수가 양수가 아니라면 입력을 종료한다.

import java.util.Scanner;

public class Main {
    public static  void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("양의 정수를 입력하세요 : ");
        int x = in.nextInt();
        int sum = 0;
        do{
            if (x % 2 == 0)
                sum += x;
            System.out.print("양의 정수를 입력하세요 : ");
            x = in.nextInt();
        }while ( x > 0);
        System.out.println("입력한 양의 정수 중에서 짝수의 합은"+sum);
    }
}

Q) 04_다음 실행 결과를 출력하는 프로그램을 for문을 사용해 작성하라.

public class Main {
    public static  void main(String[] args){
        for(int i = 1; i <= 5; i++){
            for(int j = 0; j < i; j++)
                System.out.print("*");
            System.out.println( );
        }
    }
}

Q) 05_각 변의 길이 합이 20이하이며 각 변의 길이가 정수인 직각 삼각형의 모든 변을 구하라.

public class Main {
    public static  void main(String[] args){
        int count = 0;
        for(int a = 1; a < 20; a++){
            for(int b = 1; b < 20; b++){
                for(int c = 1; c < 20; c++){
                    if ( a*a + b*b == c*c){
                        count++;
                        System.out.printf("직사각형%d: %d, %d, %d", count,a,b,c);
                        System.out.println();
                    }
                }
            }
        }
    }
}

Q) 06_철수와 영희가 가위(s), 바위(r), 보(p) 게임을 한다. r, p, s 중 하나를 입력해 승자 또는 무승부를 출력하는 프로그램을 작성하라.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("철수 : ");
        String x = in.next();
        System.out.print("영희 : ");
        String y = in.next();

        if (x.equals("r")) {
            switch (y) {
                case "r":
                    System.out.println("무승부");
                    break;
                case "s":
                    System.out.println("철수, 승!");
                    break;
                case "p":
                    System.out.println("영희, 승!");
                    break;
            }
        }else if (x.equals("s")) {
            switch (y) {
                case "r":
                    System.out.println("영희, 승!");
                    break;
                case "s":
                    System.out.println("무승부");
                    break;
                case "p":
                    System.out.println("철수, 승!");
                    break;
            }
        }else if(x.equals("p")) {
            switch (y) {
                case "r":
                    System.out.println("철수, 승!");
                    break;
                case "s":
                    System.out.println("영희, 승!");
                    break;
                case "p":
                    System.out.println("무승부");
                    break;
            }
        }
    }
}

Q) 06,07_에서 프롬프트와 r, p, s를 입력하는 부분, 입력된 데이터에 따라 승자를 출력하는 부분을 각각 메서드로 작성하라. 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String c = input("철수", in);
        String y = input("영희", in);
        whoWin(c, y);
        in.close();
    }

    public static String input(String s, Scanner in) {
        System.out.print(s + " : ");
        return in.next();
    }

    public static void whoWin(String x, String y) {
        if (x.equals(y)) {
            System.out.println("무승부");
        } else if ((x.equals("r") && y.equals("s")) || (x.equals("s") && y.equals("p")) || (x.equals("p") && y.equals("r"))) {
            System.out.println("철수, 승!");
        } else {
            System.out.println("영희, 승!");
        }
    }
}

 

Q) 08_factorial() 메서드를 화살표 case 레이블을 가진 switch문으로 작성하라.

public class Main {
    public static void main(String[] args) {
        System.out.println(factorial(5));
    }

    static int factorial(int n){
        // 코드 작성
        return switch ( n % 2 ){
            case 0 -> 2;
            default n * factorial(n-1);
        };
    }
}

 

Q) 09_foo() 메서드를 완성하라.

public class Main {
    public static void main(String[] args) {
        foo("안녕", 1);
        foo("안녕하세요", 1, 2);
        foo("잘 있어");
    }

    static void foo(String s, int x, int y) {
        System.out.println(s + " " + x + " " + y);
    }

    static void foo(String s, int x) {
        System.out.println(s + " " + x);
    }

    static void foo(String s) {
        System.out.println(s);
    }
}

Q) 10_isPrime() 메서드를 완성하라. 여기서 소수는 1보다 크면서 1과 자신 외에는 나누지 않는 수이다.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.print("양의 양의 정수를 입력하세요 : ");
        int num = new Scanner(System.in).nextInt();

        if (isPrime(num))
            System.out.println(num + "는 소수입니다.");
        else
            System.out.println(num + "는 소수가 아닙니다.");
    }

    static boolean isPrime(int n){
        for (int i = 2; i < n; i++){
            if (n % i == 0)
                return false;   //소수가 아님
        }
        return true;            //소수가 맞음
    }
}