대학교 수업/Java 프로그래밍

[JAVA] 명품 자바 프로그래밍 3장 실습문제

Jongung 2022. 4. 15. 17:00

1번

int sum = 0,  i = 0;
while (i < 100){
    sum += i;
    i += 2;
}
System.out.print(sum);

(1) 무엇을 계산하는 코드이며 실행 결과를 출력하는 내용은?
A. 0부터 100까지의 짝수만 더하는 프로그램 (2450)

(2) 위의 코드를 main() 메소드로 만들고 WhileTest 클래스로 완성하라.

import java.util.Scanner;
public class WhileTest{

    public static void main(String[] args){
        int sum = 0,  i = 0;
        while (i < 100){
            sum += i;
            i += 2;
        }
        System.out.print(sum);
    }
}

 

(3) for 문을 이용하여 동일하여 실행되는 ForTest 클래스를 작성하라.

import java.util.Scanner;
public class ForTest{

    public static void main(String[] args){
        int sum = 0,  i = 0;
        for(; i<100;){
            sum += i;
            i += 2;
        }
        System.out.print(sum);
    }
}

 

(4) do~while 문을 이용하여 동일하게 실행되는 DoWhileTest 클래스를 작성하라.

import java.util.Scanner;
public class DoWhileTestClass{

    public static void main(String[] args){
        int sum = 0,  i = 0;
        do{
            sum += i;
            i += 2;
        }while(i<100);
        System.out.print(sum);
    }
}

 

2번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        int n [][] = {{1}, {1, 2, 3}, {1,2,3,4}, {1,2}};
        for(int i = 0; i < n.length; i++){
            for(int j = 0; j < n[i].length; j++){
                System.out.print(n[i][j] + " ");
            }
            System.out.println();
        }
    }
}

2번

 

3번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        System.out.print("정수를 입력하세요>> ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = n; i > 0; i--){
            for(int j = 0; j < i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

3번

 

4번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char c = str.charAt(0);
        for(int ch1 = 0; ch1 <= c - 'a'; ch1++){
            for(char ch2 = 'a'; ch2 <= c - ch1; ch2++){
                System.out.print(ch2);
            }
            System.out.println();
        }
    }
}

4번

 

5번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        int n[] = new int [10];
        Scanner in = new Scanner(System.in);
        System.out.print("양의 정수 10개를 입력하세요 >> ");
        for(int i = 0; i < 10; i++){
            n[i] = in.nextInt();
        }
        System.out.print("3의 배수는 ");
        for(int i = 0; i < 10; i++){
            if(n[i] % 3 == 0){
                System.out.print(n[i] + " ");
            }
        }
    }
}

5번

 

6번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        int [] unit = {50000, 10000, 1000, 500, 100, 50, 10, 1};
        Scanner in = new Scanner(System.in);
        int cnt = 0;
        int nCnt = 0;
        System.out.print("금액을 입력하시오 >> ");
        int money = in.nextInt();
        while(true){
            if(money <= 0){
                break;
            }
            if(money >= unit[cnt]){
                money -= unit[cnt];
                nCnt++;
                if(money < unit[cnt]){
                    System.out.print(unit[cnt] + "원 짜리 : " + nCnt + "개\n");
                    nCnt = 0;
                    cnt++;
                }
            }
            else{
                cnt++;
            }
        }
    }
}

6번

 

7번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        int num [] = new int [10];
        double sum = 0;
        System.out.print("랜덤한 정수들 : ");
        for(int i = 0; i < num.length; i++){
            int x = (int)(Math.random()*10 + 1);
            sum += x;
            System.out.print(x+ " ");
        }
        System.out.print("\n평균은? " + sum / 10);
    }
}

7번

 

8번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("정수 몇개?");
        int n = in.nextInt();
        for(int i = 1; i <= n; i++){
            int x = (int)(Math.random()*100);
            System.out.print(x + " ");
            if(i % 10 == 0){
                System.out.println();
            }
        }
    }
}

8번

 

9번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int arr [][] = new int[4][4];
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                arr[i][j] = (int)(Math.random()*10 + 1);
            }
        }

        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

9번

 

10번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int arr [][] = new int[4][4];
        int result = 0;
        while(result < 10){
            int p1 = (int)(Math.random()*4);
            int p2 = (int)(Math.random()*4);
            if(arr[p1][p2] == 0){
                arr[p1][p2] = (int)(Math.random()*10 + 1);
                result++;
            }
        }

        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

10번

 

11번

import java.util.Scanner;
public class Average{

    public static void main(String[] args){
        int sum = 0;
        for(int i = 0; i < args.length; i++){
            sum += Integer.parseInt(args[i]);
        }
        System.out.println(sum / args.length);
    }
}

 

12번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        int sum = 0;
        for(int i = 0; i < args.length; i++){
            try{
                sum += Integer.parseInt(args[i]);
            }
            catch (NumberFormatException e){
                continue;
            }
        }
        System.out.println(sum / args.length);
    }
}

 

13번

import java.util.Scanner;
public class main{

    public static void main(String[] args){
        for(int i = 1; i< 100; i++){
            int t1 = i % 10;
            int t2 = i / 10;
            if((t1 == 3 || t1 == 6 || t1 == 9) && (t2 == 3 || t2 == 6 || t2 == 9)){
                System.out.println(i + " 박수 짝짝");
            }
            else if(t1 == 3 || t1 == 6 || t1 == 9 || t2 == 3 || t2 == 6 || t2 == 9){
                System.out.println((i + " 박수 짝"));
            }
        }
    }
}

13번

 

14번

import java.util.Scanner;
public class main{

    public static void main(String[] args) {
        String course[] = {"Java", "C++", "HTML5", "컴퓨터구조", "안드로이드"};
        int score[] = {95, 88, 76, 62, 55};
        Scanner in = new Scanner(System.in);

        while (true){
            System.out.print("과목 이름>>");
            String s = in.next();
            if(s.equals("그만")){
                break;
            }
            int isFind = 0;
            for (int i = 0; i < course.length; i++) {
                if (course[i].equals(s)) {
                    System.out.println(course[i] + "의 점수는 " + score[i]);
                    isFind = 1;
                }
            }
            if(isFind == 0){
                System.out.println("없는 과목입니다.");
            }
        }
    }
}

14번

 

15번

import java.util.InputMismatchException;
import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while (true){
            System.out.print("곱하고자 하는 두 수 입력 >> ");
            try{
                int n = in.nextInt();
                int m = in.nextInt();
                System.out.print(n+"x"+m+"="+n*m);
                break;
            }
            catch (InputMismatchException e){
                System.out.println("실수는 입력하면 안됩니다.");
                in.nextLine();
            }
        }

        in.close();
    }
}

15번

16번

import java.util.InputMismatchException;
import java.util.Scanner;
public class main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String str[] = {"가위", "바위", "보"};
        System.out.println("컴퓨터와 가위 바위 보 게임을 합니다.");
        while(true){
            int n = (int)(Math.random()*3);
            System.out.print("가위 바위 보!>>");
            String pstr = in.next();
            if(pstr.equals("그만")){
                System.out.print("게임을 종료합니다.");
                break;
            }
            if(str[n].equals(pstr)){
                System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 비겼습니다.");
            }
            else {
                if(str[n].equals("바위")){
                    if(pstr.equals("보")){
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 이겼습니다.");
                    }
                    else{
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 졌습니다.");
                    }
                }
                else if(str[n].equals("가위")){
                    if(pstr.equals("바위")){
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 이겼습니다.");
                    }
                    else{
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 졌습니다.");
                    }
                }
                else if(str[n].equals("보")){
                    if(pstr.equals("가위")){
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 이겼습니다.");
                    }
                    else{
                        System.out.println("컴퓨터 = " + str[n] + ", 사용자 = " + pstr + ", 졌습니다.");
                    }
                }
            }

        }
        in.close();
    }
}

16번