본문 바로가기
데이터 [Data]/Java & JSP

[이것이 자바다] '4장. 조건문과 반복문' 확인문제

by 냉철하마 2021. 4. 29.

1. 조건문과 반복문의 종류

 1) 조건문: if, switch

 2) 반복문: for, while, do-while

 

2. 틀린 것 고르기

 1) if문은 조건식의 결과에 따라 실행 흐름을 달리할 수 있다.

 2) switch문에서 사용할 수 있는 변수의 타입은 int, double이 될 수 있다.

 3) for문은 카운터 변수로 지정한 횟수만큼 반복시킬 때 사용할 수 있다.

 4) break문은 switch, for, while문을 종료시킬 때 사용할 수 있다.

 - 정답 4: break문은 반복문인 for, while, do-while문을 실행 중지할 때 사용 (switch문 아님)

 

3. for문을 이용하여 3의 배수의 총합을 구하는 코드 작성

package com.hb.ch04;

 

public class Exercise3 {

           public static void main(String[] args) {

                     

                      int sum = 0;

                      int i = 0;

                     

                      for(i = 0; i <= 100; i += 3) {

                                  sum += i;

                      }

                      System.out.println("3의 배수의 합: " + sum);

           }

}

 

4. while문과 Math.random() 메소드를 이용하여 주사위 (1, 2)의 형태로 작성

  , 눈의 합이 5가 아니면 계속 주사위를 던지고 5이면 실행을 멈춤

package com.hb.ch04;

 

public class Exercise4 {

           public static void main(String[] args) {

                     

                      while(true) {

                                  int n1 = (int)(Math.random()*6) + 1;

                                  int n2 = (int)(Math.random()*6) + 1;

                                  System.out.println("(" + n1 + ", " + n2 + ")");

                                  if(n1+n2 == 5) {

                                             break;

                                  }

                      }

           }

}

 

 

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

   , x y 10 이하의 자연수이다.

package com.hb.ch04;

 

public class Exercise5 {

           public static void main(String[] args) {

 

                      for (int x = 0; x <= 10; x++) {

                                 

                                  for (int y = 0; y <= 10; y++) {

                                            

                                                        if (((4 * x) + (5 * y)) == 60) {

                                                                   System.out.println("(" + x + ", " + y + ")");

                                                        }

 

                                  }

                      }

           }

}

 

 

 

6. for문을 이용해서 삼각형을 출력하는 코드를 작성하기

package com.hb.ch04;

 

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. while문과 Scanner를 이용하여 예금, 출금, 조회, 종료 기능을 제공하는 코드 작성

package com.hb.ch04;

 

import java.util.Scanner;

 

public class Exercise7 {

           public static void main(String[] args) {

                      boolean run = true;

                     

                      int balance = 0;

                     

                      Scanner scanner = new Scanner(System.in);

                     

                      while(run) {

                                  System.out.println("---------------------------------");

                                  System.out.println("1. 예금 | 2. 출금 | 3. 잔고 | 4. 종료");

                                  System.out.println("---------------------------------");

                                  System.out.println("선택> ");

                                 

                                  int num = scanner.nextInt();

                                  int money = 0;

                                 

                                  if(num == 1) {

                                             System.out.println("예금액 > ");

                                             money = scanner.nextInt();

                                             balance += money;

                                  } else if(num == 2) {

                                             System.out.println("예금액 > ");

                                             money = scanner.nextInt();

                                             balance -= money;

                                  } else if(num == 3) {

                                             System.out.println("잔고 > " + balance);

                                  } else {

                                             run = false;

                                  }

                      }

                     

                      scanner.close();

                     

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

           }

}

댓글