TIL(Today I Learned)_230718

Aspyn Choiยท2023๋…„ 7์›” 18์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
5/37

โ˜๐Ÿป์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ

1. switch

switch๋ฌธ์ด๋ž€?

  1. ์‹ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๋งŽ์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์กฐ๊ฑด๋ฌธ
    -> ์‹์˜ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ๊ฐ’๊ณผ ๊ฐ™์€ case ๊ตฌ๋ฌธ์ด ์ˆ˜ํ–‰๋œ๋‹ค.
  2. ์ž‘์„ฑ๋ฒ•
switch(์กฐ๊ฑด๋ฌธ) {
	 * 
	 * case ๊ฒฐ๊ณผ๊ฐ’1 : ์ˆ˜ํ–‰์ฝ”๋“œ1; break;
	 * case ๊ฒฐ๊ณผ๊ฐ’2 : ์ˆ˜ํ–‰์ฝ”๋“œ2; break;
	 * ...
	 * 
	 * default : case๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•˜์ง€์•Š์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ;
	 * }

2. ์ œ์–ด๋ฌธ ์—ฐ์Šต

- ์—ฐ์Šต1

ํ‚ค๋ณด๋“œ๋กœ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„
1์ด๋ฉด "๋นจ๊ฐ•์ƒ‰"
2์ด๋ฉด "์ฃผํ™ฉ์ƒ‰"
3์ด๋ฉด "๋…ธ๋ž€์ƒ‰"
4์ด๋ฉด "์ดˆ๋ก์ƒ‰"
1~4 ์‚ฌ์ด์˜ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด "ํฐ์ƒ‰" ์ถœ๋ ฅ

	public class SwitchExample {
	
	Scanner sc = new Scanner(System.in);
	
	public void ex1(){
		
		System.out.print("์ •์ˆ˜ ์ž…๋ ฅ : ");
		int input = sc.nextInt();
		
		String result;
		
		switch(input) {
		
		case 1 : result = "๋นจ๊ฐ•์ƒ‰"; break;
		case 2 : result = "์ฃผํ™ฉ์ƒ‰"; break;
		case 3 : result = "๋…ธ๋ž€์ƒ‰"; break;
		case 4 : result = "์ดˆ๋ก์ƒ‰"; break;
		
		default : result = "ํฐ์ƒ‰";
		}
		
		System.out.println(result);
	}

- ์—ฐ์Šต2

	public void ex2() {
		
		System.out.print("์›” ์ž…๋ ฅ : ");
		int month = sc.nextInt();
		String result;
		
		switch(month) {
		
		case 3 : case 4 : case 5 : result = "๋ด„"; break;
		case 6 : case 7 : case 8 : result = "์—ฌ๋ฆ„"; break;
		case 9 : case 10 : case 11 : result = "๊ฐ€์„"; break;
		case 12 : case 1 : case 2 : result = "๊ฒจ์šธ"; break;
		default : result = "์ž˜๋ชป์ž…๋ ฅ";
		
		}
		
		System.out.println(result);
	}

~ ํ˜ผ์ž์ฝ”๋”ฉํ•ด๋ณด๊ธฐ ~

- ์—ฐ์Šต3

ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ์ •์ˆ˜๊ฐ€ ์–‘์ˆ˜์ด๋ฉด์„œ ์ง์ˆ˜์ผ ๋•Œ๋งŒ โ€œ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.โ€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ 
์ง์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด โ€œํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.โ€œ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
์–‘์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด โ€œ์–‘์ˆ˜๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.โ€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
[์‹คํ–‰ ํ™”๋ฉด 1]
์ˆซ์ž๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : 8
์ง์ˆ˜์ž…๋‹ˆ๋‹ค.
[์‹คํ–‰ ํ™”๋ฉด 2]
์ˆซ์ž๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : 17
ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.
[์‹คํ–‰ ํ™”๋ฉด 3]
์ˆซ์ž๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : -3
์–‘์ˆ˜๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.

	public void ex3() {
		System.out.print("์ˆซ์ž๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int input = sc.nextInt();
		
		String result;
		
		if(input < 0) {
			result = "์–‘์ˆ˜๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.";
		}else if(input % 2 == 0 ) {
			result = "์ง์ˆ˜์ž…๋‹ˆ๋‹ค.";
		}else {
			result = "ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.";
		}
		
		System.out.println(result);
	}

- ์—ฐ์Šต4

๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์„ธ ๊ณผ๋ชฉ์˜ ์ ์ˆ˜๋ฅผ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋ฐ›๊ณ  ํ•ฉ๊ณ„์™€
ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๊ณ  ํ•ฉ๊ณ„์™€ ํ‰๊ท ์„ ์ด์šฉํ•˜์—ฌ ํ•ฉ๊ฒฉ / ๋ถˆํ•ฉ๊ฒฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
(ํ•ฉ๊ฒฉ ์กฐ๊ฑด : ์„ธ ๊ณผ๋ชฉ์˜ ์ ์ˆ˜๊ฐ€ ๊ฐ๊ฐ 40์  ์ด์ƒ์ด๋ฉด์„œ ํ‰๊ท ์ด 60์  ์ด์ƒ์ผ ๊ฒฝ์šฐ)
ํ•ฉ๊ฒฉ ํ–ˆ์„ ๊ฒฝ์šฐ ๊ณผ๋ชฉ ๋ณ„ ์ ์ˆ˜์™€ ํ•ฉ๊ณ„, ํ‰๊ท , โ€œ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค, ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!โ€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ 
๋ถˆํ•ฉ๊ฒฉ์ธ ๊ฒฝ์šฐ์—๋Š” โ€œ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.โ€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

	public void ex4() {
		System.out.print("๊ตญ์–ด์ ์ˆ˜ : ");
		int kor = sc.nextInt();
		System.out.print("์ˆ˜ํ•™์ ์ˆ˜ : ");
		int math = sc.nextInt();
		System.out.print("์˜์–ด์ ์ˆ˜ : ");
		int eng = sc.nextInt();
		
		int total = kor + math + eng;
		double avg = (kor + math + eng) / 3;
		String result;
		
		if(kor >= 40 && math >= 40 && eng >= 40 && avg >= 60) {
			System.out.println("ํ•ฉ๊ณ„ : " + total);
			System.out.printf("ํ‰๊ท  : %.1f\n", avg);
			System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค, ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!");
		}else {
			System.out.println("๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค");
		}
	}

- ์—ฐ์Šต5

: ์ฒ˜์Œ์— case๋‚˜์—ด์„ ์žŠ์–ด์„œ case๋ฅผ 12๊ฐœ์ค„๋กœ ๋งŒ๋“ค์—ˆ์—ˆ๋‹คใ… ใ… 
์Šค์œ„์น˜๋ฌธ ์กฐ๊ธˆ ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผํ• ๊ฒƒ๊ฐ™๋‹ค.

1~12 ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ•ด๋‹น ๋‹ฌ์˜ ์ผ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.(2์›” ์œค๋‹ฌ์€ ์ƒ๊ฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)
์ž˜๋ชป ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ โ€œOO์›”์€ ์ž˜๋ชป ์ž…๋ ฅ๋œ ๋‹ฌ์ž…๋‹ˆ๋‹ค.โ€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”. (switch๋ฌธ ์‚ฌ์šฉ)
[์‹คํ–‰ํ™”๋ฉด 1]
1~12 ์‚ฌ์ด์˜ ์ •์ˆ˜ ์ž…๋ ฅ : 8
8์›”์€ 31์ผ๊นŒ์ง€ ์žˆ์Šต๋‹ˆ๋‹ค.
[์‹คํ–‰ํ™”๋ฉด 2]
1~12 ์‚ฌ์ด์˜ ์ •์ˆ˜ ์ž…๋ ฅ : 99
99์›”์€ ์ž˜๋ชป ์ž…๋ ฅ๋œ ๋‹ฌ์ž…๋‹ˆ๋‹ค.

	public void ex5() {
		System.out.print("1~12 ์‚ฌ์ด์˜ ์ •์ˆ˜ ์ž…๋ ฅ : ");
		int month = sc.nextInt();
		String result;
		
		switch(month) {
		
		case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : 
			result = (month + "์›”์€ 31์ผ๊นŒ์ง€ ์žˆ์Šต๋‹ˆ๋‹ค.");
			break;
		case 2 :  
			result = (month + "์›”์€ 28์ผ๊นŒ์ง€ ์žˆ์Šต๋‹ˆ๋‹ค."); 
			break;
		case 4 : case 6 : case 9 : case 11 : 
			result = "4์›”์€ 30์ผ๊นŒ์ง€ ์žˆ์Šต๋‹ˆ๋‹ค."; 
			break;
		default : 
			result = (month + "๋Š” ์ž˜๋ชป ์ž…๋ ฅ๋œ ๋‹ฌ์ž…๋‹ˆ๋‹ค.");
		
		}
		
		System.out.println(result);
	}

- ์—ฐ์Šต6

ํ‚ค, ๋ชธ๋ฌด๊ฒŒ๋ฅผ double๋กœ ์ž…๋ ฅ ๋ฐ›๊ณ  BMI์ง€์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ
์ €์ฒด์ค‘/์ •์ƒ์ฒด์ค‘/๊ณผ์ฒด์ค‘/๋น„๋งŒ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.
BMI = ๋ชธ๋ฌด๊ฒŒ / (ํ‚ค(m) * ํ‚ค(m))
BMI๊ฐ€ 18.5๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ ์ €์ฒด์ค‘ / 18.5์ด์ƒ 23๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ ์ •์ƒ์ฒด์ค‘
BMI๊ฐ€ 23์ด์ƒ 25๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ ๊ณผ์ฒด์ค‘ / 25์ด์ƒ 30๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ ๋น„๋งŒ
BMI๊ฐ€ 30์ด์ƒ์ผ ๊ฒฝ์šฐ ๊ณ ๋„ ๋น„๋งŒ
[์‹คํ–‰ ํ™”๋ฉด]
ํ‚ค(m)๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : 1.65
๋ชธ๋ฌด๊ฒŒ(kg)๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : 58.4
BMI ์ง€์ˆ˜ : 21.45087235996327
์ •์ƒ์ฒด์ค‘

	public void ex6() {
		System.out.print("ํ‚ค(m)๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : ");
		double height = sc.nextDouble();
		
		System.out.print("๋ชธ๋ฌด๊ฒŒ(kg)๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : ");
		double weight = sc.nextDouble();
		
		double bmi = weight / (height * height);
		System.out.println("BMI ์ง€์ˆ˜ : " + bmi);
		
		String result;
		
		if (bmi < 18.5) {
			result = "์ €์ฒด์ค‘";
		}else if(bmi >= 30) {
			result = "๊ณ ๋„ ๋น„๋งŒ";
		}else if(bmi < 23) {
			result = "์ •์ƒ์ฒด์ค‘";
		}else if(bmi < 25) {
			result = "๊ณผ์ฒด์ค‘";
		}else {
			result = "๋น„๋งŒ";
		}
		
		System.out.println(result);
		
	}

- ์—ฐ์Šต7

: ์ œ์ผ ์–ด๋ ค์› ๋˜ ๋ฌธ์ œใ… ใ…  ์ฝ”๋”ฉํ•˜๋ฉด์„œ ์ด๊ฒŒ๋งž๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ ํ’€์ด๋“ค์–ด๋ณด๋‹ˆ ๋งž์•˜๋‹ค!
์ฃผ๋ง์— ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€์–ด๋ณผ๊ฒƒ.

์ค‘๊ฐ„๊ณ ์‚ฌ, ๊ธฐ๋ง๊ณ ์‚ฌ, ๊ณผ์ œ์ ์ˆ˜, ์ถœ์„ํšŸ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ  Pass ๋˜๋Š” Fail์„ ์ถœ๋ ฅํ•˜์„ธ์š”.
ํ‰๊ฐ€ ๋น„์œจ์€ ์ค‘๊ฐ„๊ณ ์‚ฌ 20%, ๊ธฐ๋ง๊ณ ์‚ฌ 30%, ๊ณผ์ œ 30%, ์ถœ์„ 20%๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ 
์ด ๋•Œ, ์ถœ์„ ํšŸ์ˆ˜๋Š” ์ด ๊ฐ•์˜ ํšŸ์ˆ˜ 20ํšŒ ์ค‘์—์„œ ์ถœ์„ํ•œ ๋‚ ๋งŒ ๋”ฐ์ง„ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•˜์„ธ์š”.
70์  ์ด์ƒ์ผ ๊ฒฝ์šฐ Pass, 70์  ๋ฏธ๋งŒ์ด๊ฑฐ๋‚˜ ์ „์ฒด ๊ฐ•์˜์— 30% ์ด์ƒ ๊ฒฐ์„ ์‹œ Fail์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

	public void ex7() {
		System.out.print("์ค‘๊ฐ„ ๊ณ ์‚ฌ ์ ์ˆ˜ : ");
		int midTerm = sc.nextInt();
		
		System.out.print("๊ธฐ๋ง ๊ณ ์‚ฌ ์ ์ˆ˜ : ");
		int finalTerm = sc.nextInt();
		
		System.out.print("๊ณผ์ œ ์ ์ˆ˜ : ");
		int task = sc.nextInt();
		
		System.out.print("์ถœ์„ ํšŸ์ˆ˜ : ");
		int attendance = sc.nextInt();

		midTerm *= 0.2;
		finalTerm *= 0.3;
		task *= 0.3;
		System.out.println("================= ๊ฒฐ๊ณผ =================");
		
		if(attendance <= 20 * 0.3) {
			System.out.println("Fail [์ถœ์„ ํšŸ์ˆ˜ ๋ถ€์กฑ (" + attendance + "/20" + ")]");
		} else {
			System.out.printf("์ค‘๊ฐ„ ๊ณ ์‚ฌ ์ ์ˆ˜(20) : %.1f\n", midTerm);
			System.out.printf("๊ธฐ๋ง ๊ณ ์‚ฌ ์ ์ˆ˜(30) : %.1f\n", finalTerm);
			System.out.printf("๊ณผ์ œ ์ ์ˆ˜ 	(30) : %.1f\n", task);
			System.out.printf("์ถœ์„ ์ ์ˆ˜	(20) : %.1f\n", attendance);
			
			double totalScore = midTerm + finalTerm + task + attendance;
			System.out.printf("์ด์  : %.1f\n", totalScore);
			
			if (totalScore >= 70) {
				System.out.println("PASS");
			}else if(totalScore < 70) {
				System.out.println("Fail [์ ์ˆ˜ ๋ฏธ๋‹ฌ]");
			}
		}
	}

3. ๋ฐ˜๋ณต๋ฌธ

: ์ œ์–ด๋ฌธ ์ค‘ ํ•˜๋‚˜๋กœ ํŠน์ • ๋ฌธ์žฅ๋“ค์„ ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋ฉฐ,for / while์œผ๋กœ ๋‚˜๋ˆ ์ง
: ๋์ด ์ •ํ•ด์ ธ์žˆ๋Š”(ํšŸ์ˆ˜๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ๋Š”) ๋ฐ˜๋ณต๋ฌธ > for๋ฌธ
: ๋์ด ์—†์ด ๊ณ„์† ๋ˆ๋‹ค > while๋ฌธ

- for๋ฌธ

  1. ์ž‘์„ฑ๋ฒ•
for (์ดˆ๊ธฐ(ํ™”)์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹) {
	๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ;
}
  • ์ดˆ๊ธฐ์‹ : for๋ฌธ์„ ์ œ์–ดํ•˜๋Š” ์šฉ๋„์˜ ๋ณ€์ˆ˜ ์„ ์–ธ
  • ์กฐ๊ฑด์‹ : for๋ฌธ์˜ ๋ฐ˜๋ณต ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•˜๋Š” ์‹, ๋ณดํ†ต ์ดˆ๊ธฐ์‹์— ์‚ฌ์šฉ๋œ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด์‹ ์ž‘์„ฑ
  • ์ฆ๊ฐ์‹ : ์ดˆ๊ธฐ์‹์— ์‚ฌ์šฉ๋œ ๋ณ€์ˆ˜๋ฅผ for๋ฌธ์ด ๋๋‚  ๋•Œ ๋งˆ๋‹ค ์ฆ๊ฐ ๋˜๋Š” ๊ฐ์†Œ์‹œ์ผœ ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€ํ•˜๊ฒŒ ํ•˜๋Š” ์‹
  • ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ : ์กฐ๊ฑด์‹์ด true ์ผ ๋•Œ๋งŒ ๋ฐ˜๋ณตํ•œ๋‹ค
  1. ์ฝ”๋“œ์˜ ํ๋ฆ„ ์ฝ๋Š” ๋ฒ•
    1ํšŒ์ „ : ์ดˆ๊ธฐ์‹ ํ™•์ธ > ์กฐ๊ฑด์‹ ํ™•์ธ
    -> ์กฐ๊ฑด์‹์ด true๋ฉด ๋ฌธ์žฅ์ˆ˜ํ–‰ ํ›„ ์ฆ๊ฐ์‹ ์—ฐ์‚ฐ
    -> ์กฐ๊ฑด์‹์ด false๋ฉด ์ˆ˜ํ–‰ํ•˜์ง€์•Š์Œ
    2ํšŒ์ „ : ์กฐ๊ฑด์‹ ํ™•์ธ
    -> ์กฐ๊ฑด์‹์ด true๋ฉด ๋ฌธ์žฅ์ˆ˜ํ–‰ ํ›„ ์ฆ๊ฐ์‹ ์—ฐ์‚ฐ
    -> ์กฐ๊ฑด์‹์ด false๋ฉด ์ˆ˜ํ–‰ํ•˜์ง€์•Š์Œ
    3ํšŒ์ „~ : 2ํšŒ์ „ ์ด์ƒ๋ถ€ํ„ฐ๋Š” ๋ชจ๋‘ 2ํšŒ์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰
    -> ์กฐ๊ฑด์‹์ด false๊ฐ€ ๋‚˜์˜ฌ๋•Œ ๊นŒ์ง€ ๋ฌธ์žฅ ์ˆ˜ํ–‰

  2. ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

	public void ex1() {
		
		for(int i = 1; i <= 10; i++) {
			// 1~10๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅํ•˜๊ธฐ
			System.out.println(i + "์ถœ๋ ฅ");
		}
	}
	
	public void ex2() {
		//3~7๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
		
		for(int i = 3; i <= 7; i++) {
			System.out.println(i + "์ถœ๋ ฅ");
		}
	}
	
	public void ex3() {
		//2๋ถ€ํ„ฐ 15๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
		for(int i = 2; i <= 15; i++) {
			System.out.println(i);
		}
	}

	public void ex4() {
		// 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜ ๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
		
		System.out.print("์ •์ˆ˜ ์ž…๋ ฅ : ");
		int num = sc.nextInt();
		
		for(int i = 1; i <= num; i++) {
			System.out.println(i);
		}
	}
	
	public void ex5() {
		// 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜ ๊นŒ์ง€ 2์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
		System.out.print("์ •์ˆ˜ ์ž…๋ ฅ : ");
		int num = sc.nextInt();
		
		for(int i = 1; i <= num; i += 2) {
			System.out.println(i);
		}
	}
	
	public void ex6() {
		// 1.0๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์‹ค์ˆ˜๊นŒ์ง€ 0.5์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
		
		System.out.print("์‹ค์ˆ˜ ์ž…๋ ฅ : ");
		double input = sc.nextDouble();
		
		for(double i = 1.0; i <= input; i += 0.5) {
			System.out.println(i);
		}
	}
	
	public void ex7() {
		// ์˜์–ด ์•ŒํŒŒ๋ฒณ A๋ถ€ํ„ฐ Z๊นŒ์ง€ ํ•œ์ค„๋กœ ์ถœ๋ ฅ
		// ABCD...Z
		// * char ์ž๋ฃŒํ˜•์€ ๋ฌธ์žํ˜•์ด์ง€๋งŒ, ์‹ค์ œ๋กœ ์ •์ˆ˜(์œ ๋‹ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•œ๋‹ค.
		
		for(int i = 'A'; i <= 'Z'; i++) {
			System.out.print((char)i); // ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด char๋กœ ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜
		}
		
		System.out.println("\n-------------------------------------");
		
		for(char i = 'A'; i <= 'Z'; i++) {
			System.out.print(i);
		}
		
	}
	
	public void ex8() {
		// 10๋ถ€ํ„ฐ 1๊นŒ์ง€ 1์”ฉ ๊ฐ์†Œํ•˜๋ฉด์„œ ์ถœ๋ ฅ
		for(int i = 10; i >= 1; i--) {
			System.out.println(i);
		}
	}
	
  1. ์‘์šฉํ•ด๋ณด๊ธฐ
	public void ex9() {
		// for๋ฌธ ์‘์šฉ1 : ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•œ ๊ฐ’์˜ ๋ˆ„์ 
		// 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ == 55
		
		int sum = 0; // ๋ฐ˜๋ณต๋˜์–ด ์ฆ๊ฐ€๋˜๋Š” i์˜ ๊ฐ’์˜ ํ•ฉ๊ณ„๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜
					// 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด์œ  : 
					// ์•„๋ฌด๊ฒƒ๋„ ๋”ํ•˜์ง€ ์•Š์Œ์œผ๋กœ ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ
		
		for(int i = 1; i <= 10; i++) {
			sum += i;
		}
		System.out.println("ํ•ฉ๊ณ„ : " + sum);
	}
	
	public void ex10() {
		// ์ •์ˆ˜ 5๊ฐœ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์„œ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
		
		// ์ž…๋ ฅ 1 : 10
		// ์ž…๋ ฅ 2 : 20
		// ์ž…๋ ฅ 3 : 30
		// ์ž…๋ ฅ 4 : 40
		// ์ž…๋ ฅ 5 : 50
		// ํ•ฉ๊ณ„ : 150
		
		int sum = 0;
		
		for(int i = 1; i <= 5; i++) {
			System.out.print("์ž…๋ ฅ " + i + " : ");
			int input = sc.nextInt();
			
			sum += input; //sum์— ์ž…๋ ฅ๋ฐ›์€ input๊ฐ’ ๋ˆ„์ 
		}
		
		System.out.println("ํ•ฉ๊ณ„ : " + sum);
	}
	
	public void ex11() {
		// ์ •์ˆ˜๋ฅผ ๋ช‡ ๋ฒˆ ์ž…๋ ฅ๋ฐ›์„์ง€ ๋จผ์ € ์ž…๋ ฅ๋ฐ›๊ณ  
		// ์ž…๋ ฅ๋œ ์ •์ˆ˜์˜ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
		
		// ์ž…๋ ฅ ๋ฐ›์„ ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ : 3
		// ์ž…๋ ฅ 1 : 10
		// ์ž…๋ ฅ 2 : 20
		// ์ž…๋ ฅ 3 : 30
		// ํ•ฉ๊ณ„ : 60
		
		int sum = 0;
		System.out.print("์ž…๋ ฅ ๋ฐ›์„ ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ : ");
		int num = sc.nextInt();
		
		for(int i = 1; i <= num; i++) {
			System.out.print("์ž…๋ ฅ " + i + " : ");
			int input = sc.nextInt();
			
			sum += input;
		}
		
		System.out.println("ํ•ฉ๊ณ„ : " + sum);
	}

	public void ex12() {
		// 1๋ถ€ํ„ฐ 20๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์ถœ๋ ฅ
		// ๋‹จ, 5์˜ ๋ฐฐ์ˆ˜์— ()๋ฅผ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ
		// 1 2 3 4 (5) 6 7 8 9 (10)....(20)
		
		for(int i = 1; i <= 20; i++) {

			if(i % 5 == 0) {
				System.out.print("(" + i + ") ");
			}else {
				System.out.print(i + " ");
			}
		}
	}
	
	public void ex13() {
		// 1๋ถ€ํ„ฐ 20๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์ถœ๋ ฅ
		// ๋‹จ, ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์˜ ๋ฐฐ์ˆ˜๋Š” () ํ‘œ์‹œ
		
		// ๊ด„ํ˜ธ๋ฅผ ํ‘œ์‹œํ•  ๋ฐฐ์ˆ˜ : 3
		// 1 2 (3) 4 5 (6)...20

		System.out.print("๊ด„ํ˜ธ๋ฅผ ํ‘œ์‹œํ•  ๋ฐฐ์ˆ˜ : ");
		int input = sc.nextInt();
		
		for(int i = 1; i <= 20; i++) {
			
			if(i % input == 0) {
				System.out.print("(" + i + ") ");
			}else {
				System.out.print(i + " ");
			}
		}
	}
		
	public void ex14() {
		// ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅ
		// 2~9 ์‚ฌ์ด ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž…๋ ฅ๋ฐ›์•„
		// ํ•ด๋‹น ๋‹จ์„ ์ถœ๋ ฅ
		// ๋‹จ, ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊ฐ€ 2~9 ์‚ฌ์ด ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด '์ž˜๋ชป์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค' ์ถœ๋ ฅ
		
		System.out.print("๋‹จ ์ž…๋ ฅ : ");
		int dan = sc.nextInt();
		
		if( (dan <= 1) || (dan >= 9) ) {
			System.out.println("์ž˜๋ชป์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
		}else {
			for(int i = 1; i <= 9; i++) {
				System.out.printf("%d X %d = %d\n", dan, i, dan*i);
			}
		}
	}
	
	public void ex15() {
		// 19๋‹จ ์ถœ๋ ฅ๊ธฐ
		// 2~19๋‹จ ์‚ฌ์ด ๋‹จ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ x 1 ~ x 19๊นŒ์ง€ ์ถœ๋ ฅ
		// ๋‹จ, 2~19 ์‚ฌ์ด ๋‹จ์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์œผ๋ฉด '์ž˜๋ชป ์ž…๋ ฅ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.' ์ถœ๋ ฅ
		
		System.out.print("๋‹จ ์ž…๋ ฅ : ");
		int dan = sc.nextInt();
		
		if((dan <= 1) || (dan >= 20)) {
			System.out.println("์ž˜๋ชป ์ž…๋ ฅ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
		}else {
			for(int i = 1; i <= 19; i++) {
				System.out.printf("%d X %d = %d\n", dan, i, dan*i);
			}
		}
	}
	
  1. ์ˆ™์ œ
package edu.kh.control.loop;

import java.util.Scanner;

public class ForExample2 {
	Scanner sc = new Scanner(System.in);
	
	public void ex1() {
		/*
		 * ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ•œ ๊ฐœ์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ 1๋ถ€ํ„ฐ ๊ทธ ์ˆซ์ž๊นŒ์ง€์˜ ์ˆซ์ž๋“ค์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜์„ธ์š”.
			๋‹จ, ์ž…๋ ฅํ•œ ์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
			๋งŒ์ผ 1 ๋ฏธ๋งŒ์˜ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋๋‹ค๋ฉด โ€œ1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”โ€œ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
		 */
		
		System.out.print("1์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int num = sc.nextInt();
		
		if(num <= 0) {
			System.out.println("1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
		}else {
			for(int i = 1; i <= num; i++) {
			System.out.print(i + " ");
			}
		}
	}

	public void ex2() {
		/*
		 * ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ•œ ๊ฐœ์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ 1๋ถ€ํ„ฐ ๊ทธ ์ˆซ์ž๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”.
			๋‹จ, ์ž…๋ ฅํ•œ ์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
			
			1์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 4
			4 3 2 1
			
			1์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 0
			1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.
		 */
		
		System.out.print("1์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int input = sc.nextInt();
		
		if(input <= 0) {
			System.out.println("1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
		}else {
			for(int i = input; i >= 1; i--) {
				System.out.print(i + " ");
			}
		}
	}
	
	public void ex3() {
		/*
		 * 1๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊นŒ์ง€์˜ ์ •์ˆ˜๋“ค์˜ ํ•ฉ์„ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.
			ex.
			์ •์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž…๋ ฅํ•˜์„ธ์š” : 8
			1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
		 */
		
		System.out.print("์ •์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int num = sc.nextInt();
		int sum = 0;
		
		for(int i = 1; i <= num; i++) {
			sum += i;
		}
		System.out.println(sum);
	}
	
	public void ex4() {
		/*
		 * ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๊ทธ ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜์„ธ์š”.
			๋งŒ์ผ 1 ๋ฏธ๋งŒ์˜ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋๋‹ค๋ฉด โ€œ1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”โ€œ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
			
			ex.
			์ฒซ ๋ฒˆ์งธ ์ˆซ์ž : 8 
			๋‘ ๋ฒˆ์งธ ์ˆซ์ž : 4 
			4 5 6 7 8 
		 */
		
		System.out.print("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž : ");
		int num1 = sc.nextInt();
		System.out.print("๋‘ ๋ฒˆ์งธ ์ˆซ์ž : ");
		int num2 = sc.nextInt();
		
		if((num1 < 1) || (num2 < 1)) {
			System.out.println("1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
		}else if(num1 > num2){
			for(int i = num2; i <= num1 ; i++) {
				System.out.println(i);
			}
		}else {
			for(int i = num1; i <= num2 ; i++) {
				System.out.println(i);
			}
		}
	}
	
	public void ex5() {
		/*
		 * ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›์€ ์ˆซ์ž์˜ ๋‹จ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

			ex.
			์ˆซ์ž : 4
			===== 4๋‹จ =====
			4 * 1 = 4
			4 * 2 = 8
			4 * 3 = 12
			4 * 4 = 16
			4 * 5 = 20
			4 * 6 = 24
			4 * 7 = 28
			4 * 8 = 32
			4 * 9 = 36
		 */
		
		System.out.print("์ˆซ์ž : ");
		int dan = sc.nextInt();
		
		System.out.println("===== " + dan + "๋‹จ =====");
		
		for(int i = 1; i <= 9; i++) {
			System.out.printf("%d * %d = %d\n", dan, i, dan*i);
		}
	}
	
}

โœŒ๐Ÿป๋Š๋‚€์ 

์ œ์–ด๋ฌธ์€ ๋งŽ์ด... ์ •๋ง ๋งŽ์ด ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹คใ… ใ… 
์•Œ๋‹ค๊ฐ€๋„ ์—‡ํ•˜๊ณ  ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ œ์–ด๋ฌธ...

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 18์ผ

์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค. ์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ