๐งฉ ํ์ต ๋จ์ : ์ ์ด๋ฌธ(Control Flow Statement)
๐ ํ์ต ๋์ : <Java์ ์ ์ 3ํ>(๋จ๊ถ ์ฑ ์ )
โ์ ์ด๋ฌธ ์์ ์์ฉ ํ์ต ์๋ฃ(2021.12.26)
์กฐ๊ฑด๋ฌธ if, swith
์์ฉA. ํ์ ์ +, -, 0 ์ฐ์ฐ๊ธฐํธ ๋ถ์ฌ์ ์ถ๋ ฅํ๊ธฐ
package com.reminder.exercises;
import java.util.Scanner;
public class Ex15_145 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("์ ์๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์. > ");
int score = scanner.nextInt();
char grade = ' ';
char opt = ' ';
if(score >= 90) {
grade = 'A';
if(score >= 98) {
opt = '+';
} else if(score < 94) {
opt = '-';
} else {
opt = '0';
}
} else if(score >= 80) {
grade = 'B';
if(score >= 88) {
opt = '+';
} else if(score < 84) {
opt = '-';
} else {
opt = '0';
}
} else if(score >= 70) {
grade = 'C';
if(score >= 78) {
opt = '+';
} else if(score < 74) {
opt = '-';
} else {
opt = '0';
}
} else if(score >= 60) {
grade = 'D';
if(score >= 68) {
opt = '+';
} else if(score < 64) {
opt = '-';
} else {
opt = '0';
}
} else {
grade = 'F';
}
System.out.printf("๋น์ ์ ์ ์๋ %d์ ์
๋๋ค.%n", score);
System.out.printf("๋น์ ์ ํ์ ์ %c%c์
๋๋ค.%n", grade, opt);
}
}
์ ์๋ฅผ ์ ๋ ฅํด ์ฃผ์ธ์. > 88
๋น์ ์ ์ ์๋ 88์ ์ ๋๋ค.
๋น์ ์ ํ์ ์ B+์ ๋๋ค.
์์ฉ B. ์ปดํจํฐ์ ๋๋ค ๊ฐ์๋ฐ์๋ณด
๊ฐ์๋ฐ์๋ณด ๊ฒฝ์ฐ์ ์๋ง๋ค ์ซ์๋ก ์ ํํด ์งํ๋๊ณ ์๋ค.
package com.reminder.exercises;
import java.util.Scanner;
public class Ex16_150 {
public static void main(String[] args) {
System.out.print("1. ๊ฐ์, 2. ๋ฐ์, 3. ๋ณด ์ค์์ ํ๋๋ฅผ ์
๋ ฅํ์ธ์. > ");
Scanner scanner = new Scanner(System.in);
int user = scanner.nextInt();
int com = (int)(Math.random() * 3) + 1;
System.out.println("๋น์ ์ " + user + "์
๋๋ค.");
System.out.println("์ปดํจํฐ๋ " + com + "์
๋๋ค.");
switch(user-com) {
case 0:
System.out.println("๋ฌด์น๋ถ์
๋๋ค.");
break;
case 1: case -2:
System.out.println("๋น์ ์ด ์ด๊ฒผ์ต๋๋ค.");
break;
case 2: case -1:
System.out.println("๋น์ ์ด ์ก์ต๋๋ค.");
break;
}
}
}
์์ฉC. ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ ์ ๋ ฅ ๋ฐ์ ์ฑ๋ณ ํ์ธ
case '1': case '3': ์ ๋ง์น ํ break; ๊ฑฐ๋ ๊ฒ์ด ์ค์ํ๋ค. ์ฌ๊ธฐ์ break;๊ฐ ๋น ์ง๋ฉด ๊ทธ ๋ค์ ๋ฌธ์ฅ๊น์ง ์ํ๋ผ ๋์จ๋ค.
package com.reminder.exercises;
import java.util.Scanner;
public class Ex17_155 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("๋น์ ์ ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์. > ");
String regNo = scanner.nextLine();
char gender = regNo.charAt(7);
switch(gender) {
case '1': case '3':
System.out.print("๋น์ ์ ๋จ์์ด๊ณ , ");
switch(gender) {
case '1':
System.out.print("2000๋
์ด์ ์ถ์์
๋๋ค.");
break;
case '3':
System.out.print("2000๋
์ดํ ์ถ์์
๋๋ค.");
break;
}
break; //์ค์!
case '2': case '4':
System.out.print("๋น์ ์ ์ฌ์์ด๊ณ , ");
switch(gender) {
case '2':
System.out.print("2000๋
์ด์ ์ถ์์
๋๋ค.");
break;
case '4':
System.out.print("2000๋
์ดํ ์ถ์์
๋๋ค.");
break;
}
}
}
}
๋ฐ๋ณต๋ฌธ for
์์ฉA. ๋๋จธ์ง ์ฐ์ฐ์ %, ๋๋๊ธฐ ์ฐ์ฐ์ / ๋ฐ๋ณต ๊ฐ ๋น๊ต
์ํ(i%3) → ๋๋จธ์ง ์ฐ์ฐ์ %๋ฅผ ์ฌ์ฉํ๋ฉด ์ผ์ ๋ฒ์ ๊ฐ๋ค์ด ์ํ ๋ฐ๋ณต๋๋ค. 120 120 120...
๋ฐ๋ณต(i/3) → ๋๋๊ธฐ ์ฐ์ฐ์ /๋ ๊ฐ์ ๊ฐ์ด ์ฐ์ ๋ฐ๋ณต๋๋ค. 00 111 222...
์ง์(2*i), ํ์(2*i-1), ์ ๊ณฑ(i*i), ์ญ์(11-i)์ ๊ฒฝ์ฐ๋ ์์๋ณผ ์ ์๋ค.
package com.reminder.exercises;
public class Ex18_160 {
public static void main(String[] args) {
System.out.println("i\t i%3\t i/3");
System.out.println("=====================");
for(int i=1; i <= 10; i++) {
System.out.printf("%d\t %d\t %d%n", i, i%3, i/3);
}
}
}
i i%3 i/3
=======
1 1 0
2 2 0
3 0 1
4 1 1
5 2 1
6 0 2
7 1 2
8 2 2
9 0 3
10 1 3
์์ฉB. ์ค์ฒฉ ๋ฐ๋ณต๋ฌธ ์ํ ์์ ํ์ธ
("" + i + j + k);๋ฅผ ์ถ๋ ฅํ๋ค. ๋ฌธ์์ด ์์ด๋ ๊ฐ ์๋ฆฌ์๋ฅผ ๋ํ ์ ์ ๊ฐ์ด ๋์ค๊ธฐ ๋๋ฌธ์ด๋ค.
package com.reminder.exercises;
public class Ex19_164 {
public static void main(String[] args) {
for(int i=1; i <= 3; i++) {
for(int j=1; j <= 3; j++) {
for(int k=1; k <= 3; k++) {
System.out.println("" + i + j + k);
}
}
}
}
}
111
112
113
121
122
123
...
333
์์ฉC. ์ด์ค ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ชจ์ ๋ง๋ค๊ธฐ
package com.reminder.exercises;
public class Ex20_165 {
public static void main(String[] args) {
for(int i=1; i <= 5; i++) {
for(int j=1; j <= 5; j++) {
System.out.printf("[%d, %d]", i, j);
}
System.out.println();
}
}
}
[1, 1][1, 2][1, 3][1, 4][1, 5]
[2, 1][2, 2][2, 3][2, 4][2, 5]
[3, 1][3, 2][3, 3][3, 4][3, 5]
[4, 1][4, 2][4, 3][4, 4][4, 5]
[5, 1][5, 2][5, 3][5, 4][5, 5]
"%5c" 5๋งํผ์ ๊ณต๋ฐฑ ' '์ ์ถ๋ ฅํด ๋น ๊ณต๊ฐ์ ๋ผ ์ ์๋ค.
package com.reminder.exercises;
public class Ex20_165 {
public static void main(String[] args) {
for(int i=1; i <= 5; i++) {
for(int j=1; j <= 5; j++) {
if(i == j) {
System.out.printf("[%d, %d]", i, j);
} else {
System.out.printf("%5c", ' ');
}
}
System.out.println();
}
}
}
[1, 1]
[2, 2]
[3, 3]
[4, 4]
[5, 5]
์์ฉD. ํฅ์๋ for๋ฌธ(enhanced for statement)
ํํ์ ์๋์ ์ผ๋ก ๊ฐ๊ฒฐํ๋, ์ผ๋ฐ for๋ฌธ๊ณผ ๋ฌ๋ฆฌ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์ ์ ์ ์ฅ๋ ์์๋ค์ ์ฝ์ด์ฌ ์๋ง ์๋ ์ ์ฝ์ด ๋ฐ๋ฅธ๋ค.
โ for(ํ์ ๋ณ์๋ช : ๋ฐฐ์ด ๋๋ ์ปฌ๋ ์ ) {
ใใใ๋ฐ๋ณตํ ๋ฌธ์ฅ
}
package com.reminder.exercises;
public class Ex21_166 {
public static void main(String[] args) {
int[] arr = {100, 200, 300, 400, 500};
int sum = 0;
for(int i=0; i < arr.length; i++) {
System.out.printf("%d ", arr[i]);
}
System.out.println();
for(int tmp : arr) {
System.out.printf("%d ", tmp);
sum += tmp;
}
System.out.println();
System.out.println("sum = " + sum);
}
}
100 200 300 400 500
100 200 300 400 500
sum = 1500
๋ฐ๋ณต๋ฌธ while
์์ฉA. ์นด์ดํธ๋ค์ด
for๋ฌธ์ ์ง์ฐ์๊ฐ์ ๊ฐ๋ฆฌํค๋ ๋น ๋ฌธ์ฅ์ด๋ค.
package com.reminder.exercises;
public class Ex22_169 {
public static void main(String[] args) {
int i = 6;
System.out.println("์นด์ดํธ๋ค์ด์ ์์ํฉ๋๋ค!");
while(i-- != 0) {
System.out.println(i);
for(int j=0; j < 2_000_000_000; j++) {
;
}
}
System.out.println("Happy New Year!");
}
}
์์ฉB. 369๊ฒ์
package com.reminder.exercises;
public class Ex23_174 {
public static void main(String[] args) {
for(int i=1; i <= 39; i++) {
System.out.printf("%d", i);
int tmp = i;
do {
if(tmp % 3 == 0 && tmp % 10 != 0) {
System.out.print("์ง");
}
} while((tmp /= 10) != 0);
System.out.println();
}
}
}
...
30์ง
31์ง
32์ง
33์ง์ง
34์ง
35์ง
36์ง์ง
37์ง
38์ง
39์ง์ง
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA/์์ ๊ณผ์ practice] ๋ฐฐ์ด | ๋ค์ฐจ์ ๋ฐฐ์ด Lv. 1~2 (0) | 2021.12.27 |
---|---|
[JAVA] 5-1. ๋ฐฐ์ด์ ์ ์ธ, ํ ๋น, ์ด๊ธฐํ (0) | 2021.12.27 |
[JAVA/์์ ๊ณผ์ ] ๋ฐ๋ณต๋ฌธ, ๋ถ๊ธฐ๋ฌธ practice (0) | 2021.12.26 |
[์๋ฐ์ ์ ์] Ch 4. ์ ์ด๋ฌธ ์ฐ์ต๋ฌธ์ ํ์ด (0) | 2021.12.25 |
[์๋ฐ์ ์ ์] Ch 3. ์ฐ์ฐ์ ์์ ์์ฉ ํ์ต (0) | 2021.12.25 |