본문 바로가기
Java/이것이 자바다

이자바 4장(조건문과 반복문) 확인문제

by k-mozzi 2022. 11. 23.
반응형

- 3번

package ch4;

public class exercise3 { // 3의 배수 총합을 구하는 코드 

	public static void main(String[] args) {

		int sum = 0;
		for (int i = 1; i <= 100; i++) {
			if (i % 3 == 0) {
				sum += i;
			}
		}
		System.out.println(sum);

	}

}

 

 

- 4번

package ch4;

public class exercise4 { // 주사위 합이 5가 되면 출력하는 코드 

	public static void main(String[] args) {

		while (true) {
			int a = (int) (Math.random() * 6) + 1;
			int b = (int) (Math.random() * 6) + 1;
			System.out.println("(" + a + ", " + b + ")");
			if (a + b == 5) {
				break;
			}
		}

	}

}

 

 

- 5번

package ch4;

public class exercise5 { // 방정식 4x + 5y = 60의 모든 해를 구한 후 출력하는 코드 

	public static void main(String[] args) {

		for (int x = 1; x <= 10; x++) {
			for (int y = 1; y <= 10; y++) {
				if (((4 * x) + (5 * y)) == 60) {
					System.out.println("(" + x + "," + y + ")");
				}
			}
		}

	}

}

 

 

- 6번

package ch4;

public class exercise6 { // 피라미드 형태를 출력하는 코드 

	public static void main(String[] args) {

		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}

	}

}

 

 

- 7번

package ch4;

public class exercise7 {

	public static void main(String[] args) throws Exception { // 키보드 입력에 따라 기능을 수행하는 코드 스캐너x

		boolean run = true;

		int balance = 0;

		while (run) {
			if (balance != 13 && balance != 10) {
				System.out.println("----------");
				System.out.println("1: 예금 | 2: 출금 | 3: 잔금 | 4: 종료");
				System.out.println("----------");
				System.out.print("선택> ");
			}

			balance = System.in.read();

			if (balance == 49) { // 1을 읽었을 경우
				System.out.println("예금액: 10000");
			} else if (balance == 50) { // 2를 읽었을 경우
				System.out.println("출금액: 2000");
			} else if (balance == 51) { // 3을 읽었을 경우
				System.out.println("잔금: 8000");
			} else if (balance == 52) { // 4를 읽었을 경우
				run = false;
			}
		}

		System.out.println("프로그램 종료");

	}

}

 

728x90
반응형

'Java > 이것이 자바다' 카테고리의 다른 글

이자바 6장(클래스) 확인문제  (0) 2022.11.24
이자바 5장(참조 타입) 확인문제  (0) 2022.11.23
어노테이션  (0) 2022.10.21
클래스  (2) 2022.10.13
타입 변환 메소드  (2) 2022.10.07

댓글