[K-디지털 트레이닝] 학습정리 21일차 (2024-07-16)

smoo·2024년 7월 16일

JAVA_STUDY

목록 보기
6/25

1. 1 부터 1000까지의 숫자중 11의 배수이자 7의 배수인 첫번째 수는?

public class BreakContinue {

	public static void main(String[] args) {

		for(int i=1; i<=1000; i++) {

			if(i%7==0 && i%11==0) {

				System.out.println(i);
				break;

			}
		}

2. 헬로 월드를 1000번 찍으시오.

public class DoWhileFor {s

	public static void main(String[] args) {

		// ① while 사용

		int count=1;

		while(count<= 1000) {
			System.out.println(count+"번"+": Hello world");
			count++;
		}


		// ② do while 사용

		count =1;

		do {
			System.out.println(count+"번"+": Hello world");
			count++;
		} while(count<=100);


		// ③ for문 사용

		for(int i=0; i<100; i++){
			System.out.println(i+"번"+": Hello world");
		}	
	}
}

3.switch 문에서 '걸어서 하늘까지' 를 설명하시오.

switch 설정된 부분까지 실행이 된다.


4. month 변수를 선언후 switch 문 버전과 if 문 버전 두개를 작성하시오.

345 -> 봄입니다.
678 -> 여름입니다.
9 10 11 -> 가을 입니다.
12 1 2 -> 겨울 입니다.

① switch 문 버전

public class MonthSwitch {
	public static void main(String[] args) {
		
		// 걸리면 break 까지;
		
		int month = 5;

		switch(month) {
		case 3:
		case 4:
		case 5:
			System.out.println("봄입니다.");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("여름입니다.");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("가을입니다.");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("겨울입니다.");
			break;
		default:
			System.out.println("잘못된 입력입니다.");
		}
				
	}

}

② if 문 버전

public class MonthIf {

	public static void main(String[] args) {
		
		int month = 5;
		
		if(month==3||month==4||month==5) {
			System.out.println("봄입니다.");
		} else if(month==6||month==7||month==8) { 
			System.out.println("여름입니다.");
		} else if(month==9||month==10||month==11) { 
			System.out.println("가을입니다.");
		} else if(month==12||month==1||month==2)  {
			System.out.println("겨울입니다.");
		} else {
			System.out.println("잘못된 입력값 입니다.");
		}
		
	}

}

6.아래를 프로그래밍 하시오.

- int num = -10 을 할당후 해당 정수에 대한 절대값을 출력하는 프로그램을 작성하시오.

public class MathAbs {    
	public static void main(String[] args) {
		int num = -10;       
		int absNum = Math.abs(num);        
		System.out.println(absNum);
	}
}

7. for 문에서 for 문이 실행되는 순서를 설명하시오.


8. 9단을 출력하는 프로그램을 만드시오. ( while 문 사용할 것 )

public class While {

	public static void main(String[] args) {

		//while 사용
		int dan = 9;
		int i=1;

		while (i<=9) {
			System.out.println(dan + "*"+ i + "= " + (dan*i));
			i++;
		}
	}
}

9. 1부터 100까지의 합을 구하시오.

public class DoWhileFor {

	public static void main(String[] args) {

		// 1부터 100까지의 합
		int sum=0;

		for(int i=1; i<=100; i++) {
			sum += i;
			System.out.println(i+":합:"+sum);
		}

		System.out.println(sum);

	}

}

10.1부터 100까지의 홀수들의 합을 구하시오.

public class DoWhileFor {

	public static void main(String[] args) {

		// 1부터 100까지의 홀수들의 합
		int sum=0;

		for (int i=1; i<=100 ; i+=2) {
			sum += i;
			System.out.println(i+":합:"+sum);
		}

		System.out.println(sum);

	}

}

11.반복문에서의 break 와 continue 를 설명하시오.

🟠 break문

  • break; 를 적어준 곳에서 해당 조건문 블록과 그 밖의 반복문 자체를 탈출
  • 다음 블록으로 넘어가고자 할 때 사용

🟠 continue문

  • 해당 조건문 블록을 탈출하여 아래 명령문은 실행하지 않고, 다음 반복문 실행절차를 수행
  • 다음 반복을 실행할 때 사용

12.1부터 100까지의 수중 3의 배수가 아닌값들의 합은?

public class DoWhileFor {

	public static void main(String[] args) {

		// 1부터 100까지의 수중 3의 배수가 아닌값들의 합
		int sum=0;

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

		System.out.println(sum);

	}

}

13.while 문과 do while 문의 차이는?

🟠 while문

  • 조건이 참일 때만 실행된다.
  • 처음부터 조건이 거짓이면 한번도 실행되지 않는다.

🟠 do-while문

  • 조건 상관없이 무조건 한번은 실행한다.
  • 처음에 한번 실행하고 그 다음에 조건을 확인한다.

14. 1과 1000 사이의 숫자중 5의 배수이자 7의 배수인 숫자의 갯수는?

public class BreakContinue {

	public static void main(String[] args) {
		// 1부터 1000까지의 숫자 중 5의 배수이자 7의 배수인 첫번째 수는?
		
		for(int i=1; i<=1000; i++) {
			if(i%5==0 && i%7==0) {
				System.out.println(i);
				break;
			}
		}

	}

}
profile
코딩 스터디 정리

0개의 댓글