ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [이것이 자바다] 4장 확인문제
    프로그래밍/Java 2021. 4. 29. 22:03

    3번

    /*
     * 3번
     * 3의 배수의 총합을 구하는 코드
     */
    public class Exercise03 {
    	public static void main(String[] args) {
    		int sum = 0;
    		
    		for(int i=1; i<=100; i++) {
    			if (i % 3 == 0) {
    				System.out.println("3의 배수 : " + i);
    				sum += i;
    			} 
    		}
    		System.out.println("3의 배수의 합: " + sum);
    	}
    }
    

    4번

    /*
     * 4번
     * 주사위 문제
     * 눈의 합이 5가 아니면 계속 주사위 던지기
     * 눈의 합이 5이면 실행을 멈추기
     */
    
    public class Exercise04 {
    	public static void main(String[] args) {
    		int dice_1 = (int) (Math.random() * 6 + 1); 
    		int dice_2 = (int) (Math.random() * 6 + 1); 
    		System.out.println("("+ dice_1 +","+ dice_2 +")");
    		
    		while(!(dice_1 + dice_2 == 5)) {
    			dice_1 = (int) (Math.random() * 6 + 1);
    			dice_2 = (int) (Math.random() * 6 + 1);
    			System.out.println("("+ dice_1 +","+ dice_2 +")");
    		}
    		
    		System.out.println("종료되었습니다");
    	}
    }
    

    5번

    //5번
    //4a+5b=60 의 값 구하기      제약 조건 : a 와 b는 10을 넘지 않는수
    
    public class Exercise05 {
    	public static void main(String[] args) {
    		
    		for(int i=1; i<=10; i++) {
    			for(int j=1; j<=10; j++) {
    				if( ((4*i)+(5*j) == 60) ) {
    					System.out.println("("+ i +","+ j +")");
    				}
    			}
    		}
    	}
    }
    

    6번

    /*
    6번
    *
    **
    ***
    ****
    *****
    출력하기
    */
    
    public class Exercise06 {
    	public static void main(String[] args) {
    		for(int i=1; i<=5; i++) {
    			for(int j=i; j>0; j--) {
    				System.out.print('*');
    			}
    			System.out.println();
    		}
    	}
    }
    

    7번

    /*
     * 예금,출금,잔액조회,종료 서비스
     */
    
    import java.util.Scanner;
    
    public class Exercise07 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		
    		int balance = 0;
    		boolean run = true;
    		int choice = 0;
    		
    		while(run) {
    			System.out.println("------------------------------");
    			System.out.println("1.예금 | 2.출금 | 3.잔고 | 4.종료");
    			System.out.println("------------------------------");
    			System.out.print("선택> ");
    			choice = sc.nextInt();
    		
    		
    			if(choice == 1) {
    				System.out.print("예금액> ");
    				balance += sc.nextInt();
    			}else if(choice == 2) {
    				System.out.print("출금액> ");
    				int minus = sc.nextInt();
    				if ((balance - minus) < 0) { //출금액이 초과시 부족알림 및 출금거부
    					System.out.println("잔액이 부족합니다");
    				} else {
    					balance -= minus;
    				}
    			}else if(choice == 3) {
    				System.out.println("잔고> " + balance);
    			}else if(choice == 4) {
    				run = false;
    			}else {
    				System.out.println("다시 입력해주세요");
    			}
    		}
    		sc.close();
    	}
    }
    
    /* 오류
     * 출금: else 부분에서 계속 멈춰있다. why?
     * nextInt()가 여러번 받는건 불가능한가?
     
    
    import java.util.Scanner;
    
    public class Exercise07 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		
    		int balance = 0;
    		boolean run = true;
    		int choice = 0;
    		
    		while(run) {
    			System.out.println("------------------------------");
    			System.out.println("1.예금 | 2.출금 | 3.잔고 | 4.종료");
    			System.out.println("------------------------------");
    			System.out.print("선택> ");
    			choice = sc.nextInt();
    		
    		
    			if(choice == 1) {
    				System.out.print("예금액> ");
    				balance += sc.nextInt();
    			}else if(choice == 2) {
    				System.out.print("출금액> ");
    				if ((balance - sc.nextInt()) < 0) {
    					System.out.println("잔액이 부족합니다");
    				} else {
    					balance -= sc.nextInt();
    				}
    			}else if(choice == 3) {
    				System.out.println("잔고> " + balance);
    			}else if(choice == 4) {
    				run = false;
    			}else {
    				System.out.println("다시 입력해주세요");
    			}
    		}
    	}
    }
    */

    댓글

Designed by Tistory.