MapleStory Finger Point
본문 바로가기
Back-end/Java

[Java] 혼자 공부하는 자바 160~161p 풀어보기

by 디팔⸜( ◜࿁◝ )⸝︎︎ 2022. 9. 8.

확인문제 2번

for문을 이용해서 1부터 100까지의 정수 중에서 3의 배수의 총합을 구하는 코드를 작성해보세요.

package com.example.MyJavaProject;

class Main {
    public static void main(String[] args) {
		int hap = 0;
        for(int i=0; i<100; i+=3){
        	hap += i;
        }
    System.out.print(hap);
    }
}

확인문제 3번

while문과 Math.random() 메소드를 이용하여 2개의 주사위를 던졌을 때 나오는 눈을 (눈1, 눈2)형태로 출력하고

눈의 합이 5라면 반복문이 멈추는 코드를 작성해 보세요.

package com.example.MyJavaProject;

class Main {
    public static void main(String[] args) {
        while(true){
            int firstEye = (int) (Math.random()*6 ) + 1;
            int secondEye = (int) (Math.random()*6 ) + 1;
            System.out.printf("(%d, %d) \n",firstEye,secondEye);
            if(firstEye+secondEye==5){
                System.out.println("주사위 눈의 합이 5이므로 작동을 중단합니다.");
                break;
            }
        }
    }
}


확인문제 4번

중첩 for문을 이용하여 방정식 4x + 5y = 60의 모든 해를 구하고, (x, y)와 같은 형태로 출력하세요.

(x, y는 10 이하의 자연수)

class Main {
    public static void main(String[] args) {
        for(int x=1; x<11; x++ ){
            for(int y=1; y<11; y++){
                if(((4*x)+(5*y))==60){
                    System.out.printf("x : %d, y : %d \n",x,y);
                }
            }
        }
    }
}


확인문제 5번

for문으로 *을 출력하는 코드를 작성해보세요.

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

확인문제 6번

확인문제 5번의 별이 좌우 반전으로 나오게 코드를 작성하세요.

class Main {
    public static void main(String[] args) {
        String empty = "";
        String star = "*";
        for(int i=1; i<5; i++){
            empty += star;
            System.out.printf("%4s\n",empty);
        }
    }
}

확인문제 7번

while문과 Scanner 를 이용해서 키보드로 입력된 데이터로 예금, 출금, 조회, 종료 기능을 제공하는 코드를 작성해보세요.

package exam1;
import java.util.Scanner;

public class deepalworld {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            boolean run = true;
            int balance = 0;

            while(run){
                System.out.println("---------------------");
                System.out.println("1. 예금 2. 출금 3. 잔고  4. 종료");
                System.out.println("---------------------");
                System.out.print("원하시는 기능의 숫자를 입력해주세요 >> ");
                String actNum = scanner.nextLine();
                switch (actNum){
                    case "1":
                        System.out.print("입금할 금액을 입력해주세요.");
                        String moneyIn  = scanner.nextLine();
                        balance = balance + Integer.parseInt(moneyIn);
                        break;
                    case "2":
                        System.out.print("출금할 금액을 입력해주세요. ");
                        String moneyOut = scanner.nextLine();
                        if(balance - (Integer.parseInt(moneyOut))<0){
                            System.out.println("잔액이 부족합니다.");
                            break;
                        } else {
                            balance = balance - Integer.parseInt(moneyOut);
                            break;
                        }
                    case "3":
                        System.out.printf("잔액은 %d 원 입니다. \n", balance);
                        break;
                    case "4":
                        System.out.println("시스템을 종료합니다.");
                        run = !run;
                        break;
                }
            }
        }
    }

댓글