4 II public class Sum Test public static
4장. 자바의 기본 구조 II : 선택, 반복, 배역 public class Sum. Test { public static void main(String a 1[]) { int a, b, sum; a = Integer. parse. Int(a 1[0]); b = Integer. parse. Int(a 1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System. out. println("두수의 "); System. out. println("두수의 합은 " + sum + "입니다"); } } 1
§ 목차 l section 1 선택문 - 1 -1 if문 - 1 -2 switch문 l section 2 반복문 - 2 -1 while문 - 2 -2 do-while문 - 2 -3 for문 - 2 -4 반복문의 비교와 중첩 l section 3 제어의 이동 - 3 -1 break문 - 3 -2 continue문 - 3 -3 return문 3
1 선택문 1 -1 if문 l 다중 if문(if-else if) if ( score >= 90 ) System. out. println("A학점취득 성공"); else if ( score >= 80 ) System. out. println("B학점취득 성공"); else if ( score >= 70 ) System. out. println("C학점취득 성공"); else if ( score >= 60 ) System. out. println("D학점취득 성공"); else System. out. println("학점취득 실패"); 4장. 자바의 기본 구조 II : 선택, 반복, 배역 17
1 선택문 1 -1 if문 l 실습예제 - 조건절의 결과가 거짓일 때 else if 절을 이용하여 다시 조건을 검사한다. 마지막에 더 이상 비교할 조건이 없을 경우에는 else 절만 사용하여 기술한다. Multi. IFTest. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 public class Multi. IFTest { public static void main(String args[]) { int grade = Integer. parse. Int(args[0]); if (grade >= 90) System. out. println("A학점 취득"); 다시 조건 검사 else if (grade >= 80) System. out. println("B학점 취득"); 다시 조건 검사 else if (grade >= 70) System. out. println("C학점 취득"); 다시 조건 검사 else if (grade >= 60) System. out. println("D학점 취득"); 모든 조건이 거짓일 때 수행 else System. out. println("학점 취득 실패"); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 18
1 선택문 1 -1 if문 l 실습예제 Nested. IFTest. java 14 15 16 17 18 19 20 21 22 23 24 25 26 27 { if (score >= 60) { System. out. println("보통학점군에 속합니다") if (score >= 70) if ~ else절 System. out. println("C 학점입니다"); else System. out. println("D 학점입니다"); } else System. out. println("학점취득 실패"); if ~ else절 } } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 23
1 선택문 1 -2 switch문 l 형식 : switch문 swith (num) { case 1 : System. out. println("num의 값은 1입니다"); break; case 2 : System. out. println("num의 값은 2입니다"); break; case 3 : System. out. println("num의 값은 3입니다"); break; case 4 : System. out. println("num의 값은 4입니다"); break; num의 값이 1~4 사이의 값이 아닐 경우 수행 default : System. out. println("num의 값은 1 -4 사이의 값이 아닙니다"); break; } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 27
1 선택문 1 -2 switch문 l 실습예제 - 다음 프로그램은 사용자로부터 월을 입력받아 해당하는 계절을 출력하는 프로그램이다. case 절에서 처리할 내용이 없을 경우에는 그대로 비워둔다. Switch. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 public class Switch. Test 1 { public static void main(String args[]) { int month = Integer. parse. Int(args[0]); String Mto. S; switch (month) switch 절 { case 12: case 1: case 2: case 절을 하나의 절로 묶어 수행 Mto. S = "겨울입니다. "; break; case 3: case 4: case 5: 4장. 자바의 기본 구조 II : 선택, 반복, 배역 28
1 선택문 1 -2 switch문 l 실습예제 Switch. Test 1. java 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Mto. S = break; case 6: case 7: case 8: Mto. S = break; case 9: case 10: case 11: Mto. S = break; default: Mto. S = "봄입니다. "; "여름입니다. "; "가을입니다. "; "12개월 이내의 달이 아닙니다. "; default 절 } System. out. println(month + "월은 " + Mto. S ); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 29
2 반복문 2 -1 while문 l 실습예제 - 다음 프로그램은 구구단 3단을 출력하는 프로그램이다. While. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 12 public class While. Test 2 { public static void main(String args[]) { int x = 3; int y = 1; while (y <= 9) { System. out. println(x + " * " + y + " = " + x * y); y++; } } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 while문 38
2 반복문 2 -2 do-while문 l do-while문 int a = 10, b = 20; do System. out. println("do-while문은 반복 부분이 최소한 한번은 수행 된다"); while (a > b) 처음 한 번은 무조건 수행 do { System. out. println("******* 메뉴 *******"); System. out. println("1. while 반복문"); System. out. println("2. do-while 반복문"); System. out. println("3. for 반복문"); System. out. println("끝 : 100입력"); } while(input != 100) 4장. 자바의 기본 구조 II : 선택, 반복, 배역 do while문은 메뉴를 나타낼 때 적합하다. 43
2 반복문 2 -2 do-while문 l 실습예제 - while문에서도 예제로 작성해 보았던 1부터 10까지 더하는 프로그램을 do-while문으로 작 성해 보았다. Do. While. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 public class Do. While. Test 1 { public static void main(String args[]) { int hap=0, count=1; do { do-while문 반복 부분 hap = hap + count; count = count + 1; do-while문의 조건절 } while (count <= 10); System. out. println("1부터 10까지의 합은 "+ hap + " 입니다"); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 44
2 반복문 2 -2 do-while문 l 실습예제 - 사용자로부터 입력된 값이 메뉴의 범위를 벗어나면 다시 메뉴를 보여주는 프로그램이다. Do. While. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 public class Do. While. Test 2 { public static void main(String args[]) throws java. io. IOException { char choice; do { System. out. println("***반복문 종류 설명***"); System. out. println(" 1. while 반복문"); 메뉴를 나타내는 프로그램 System. out. println(" 2. do-while 반복문"); System. out. println(" 3. for 반복문"); System. out. print("원하는 번호를 입력하세요 : "); choice = (char)System. in. read(); 키보드 입력 System. out. println(); } while( choice < '1' || choice > '3'); 조건절 switch(choice) { 4장. 자바의 기본 구조 II : 선택, 반복, 배역 46
2 반복문 2 -3 for문 l 실습예제 - while문과 do-while문에서 작성하였던 1부터 10까지의 합을 구하는 프로그램을 for문을 사 용하여 작성하였다. For. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 public class For. Test 2 { public static void main(String args[]) { int i, sum=0; for (i = 1; i <= 100 ; i = i + 2) { sum = sum + i; } System. out. println("1부터 100 사이의 홀수의 합은 " + sum + " 입니다 "); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 59
2 반복문 2 -4 반복문의 비교와 중첩 l 실습예제 Nested. Loop. While. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 public class Nested. Loop. While. Test{ public static void main(String args[]) { int i=2; while ( i<=9 ) { System. out. println("*** " + i + "단 ***"); int j=1; while ( j<=9 ) { System. out. println(i+" * " + j + " = " + i*j); j++; } System. out. println(); i++; } } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 내포된 while문 바깥쪽 while문 70
2 반복문 2 -4 반복문의 비교와 중첩 l 실습예제 Nested. Loop. Do. While. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 public class Nested. Loop. Do. While. Test{ public static void main(String args[]) { int i=2; do{ System. out. println("*** " + i + "단 ***"); int j=1; do{ System. out. println(i+" * " + j + " = " + i*j); j++; }while ( j<=9 ); System. out. println(); i++; }while ( i<=9 ); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 내포된 do-while문 바깥쪽 do-while문 72
3 제어의 이동 3 -1 break문 l break문 switch ( a+b ) { case 1: System. out. println(". . . . "); break; case 2 : System. out. println(". . . . "); break; case 3 : System. out. println(". . . . "); break; . . . default : . . break; } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 switch문 밖으로 이동 78
3 제어의 이동 3 -1 break문 l 실습예제 - while문의 조건절을 true로 설정한 다음, 반복문 내에서 특정 조건을 검사하여 반복을 벗어 나는 프로그램이다. While. Break. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 public class While. Break. Test { public static void main(String args[]) { int j=1, sum=0; while(true) 무한 반복 while문 { j의 값이 100을 넘으면 break문 수행 if(100 < j) break; sum = sum + j; j++; } System. out. println("1부터 " + (j-1) + "까지의 합 : " + sum); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 82
3 제어의 이동 3 -1 break문 l 실습예제 - 내포된 블록에서 일반 블록을 지정하여 블록을 벗어나는 예를 나타낸 프로그램이다. Break. Label. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 public class Break. Label. Test { public static void main(String args[]) { boolean t = true; First: { Second: { Third: { System. out. println("Third 블록 'break' 문장 전"); break Second문 수행 if(t) break Second; System. out. println("Third 블록 'break' 문장 후"); } System. out. println("Second 블록 문장"); } System. out. println("First 블록 문장"); } System. out. println("main 블록 문장"); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 Third 블록 선언 Second 블록 First 블록 선언 선언 89
3 제어의 이동 3 -1 break문 l 실습예제 - 두 개의 무한 반복문에서 break문과 break label 문의 사용 예를 보이고 있다. Break. Label. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 public class Break. Label. Test 2 { public static void main(String args[]) { int i=2, j; Loop : while(true) { j=1; Innerloop : while(true) { if ( j > 9 ) break; if ( i > 9 ) break Loop; System. out. println(i+” * " + j + " = " + i*j); j++; } System. out. println(); i++; } } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 Innerloop 블록으로 지정, 무한 반복 Loop 블록, 무한 반복 91
3 제어의 이동 3 -2 continue문 l 실습예제 - 내포된 for문에서 label을 이용하여 바깥의 for문으로 제어를 이동하는 프로그램 Continue. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 public class Continue. Label. Test { public static void main(String args[]) { int i, j; 반복문에 Outer 블록 선언 Outer : for ( i=2; i<=9 ; i=i+1 ) { System. out. println("*** " + i + "단 ***"); 내포된 반복문 선언 for(j=1; j<=9; j=j+1) { if (j == 3) continue Outer; System. out. println(i+" * " + j + " = " + i*j); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 96
4 배열 4 -2 배열의 초기화 및 사용 l 실습예제 Arrays. One. DTest 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 public class Arrays. One. DTest 1 { public static void main(String args[]) { int number[] = new int[10]; 10개의 요소를 가진 int형 배열 선언 System. out. println("number 배열의 길이 : " + number. length); 배열의 길이를 출력 int i, j, sum=0; double avg; for (i=0; i < number. length ; i=i+1) 배열의 각 요소에 1부터 10까지 저장 number[i] = i+1; for (j=0; j < number. length ; j=j+1) 배열의 각 요소의 합계를 구함 sum = sum + number[j]; System. out. println("1부터 10까지의 합은 " + sum + "입니다"); avg = (double)sum/number. length; System. out. println("1부터 10까지의 합의 평균은 " + avg + "입니다"); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 110
4 배열 4 -2 배열의 초기화 및 사용 l 실습예제 Arrays. One. DTest 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 public class Arrays. One. DTest 2 { public static void main(String args[]) { int score[] = {88, 97, 53, 62, 92, 68, 82}; 배열의 선언과 초기화 수행 max 변수에 첫 번째 요소 값 저장 int max=score[0]; for (int i=1; i < score. length ; i=i+1) { if ( score[i] > max ) 배열 요소 중 가장 큰 값을 max에 저장 max = score[i]; } System. out. println ("배열 요소의 최댓값은 " + max + "입니다"); } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 112
4 배열 4 -2 배열의 초기화 및 사용 l 실습예제 Multi. Array. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 public class Multi. Array. Test 1 { public static void main(String args[]) { int two_array[][]= new int[4][5]; 10개의 요소를 가진 int형 배열 선언 int i, j, k = 1; for(i=0; i<4; i++) for(j=0; j<5; j++) 배열의 각 요소에 1~20까지 저장 { two_array[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) 하나의 행을 한 라인에 출력 System. out. print(two_array[i][j] + " "); System. out. println(); } } } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 116
4 배열 4 -2 배열의 초기화 및 사용 l 실습예제 Multi. Array. Test 2. java 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 } } +two. D[1]. length); System. out. println("세 번째 행의 요소 수는 : " +two. D[2]. length); System. out. println("네 번째 행의 요소 수는 : " +two. D[3]. length); int i, j, k = 0; for(i=0 ; i < two. D. length ; i++) length 속성을 사용하여 for(j=0 ; j< two. D[i]. length ; j++) 조건절 지정 { two. D[i][j] = k; k++; } for(i=0 ; i < two. D. length ; i++) { for(j=0 ; j< two. D[i]. length; j++) System. out. print(two. D[i][j] + " "); System. out. println(); } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 119
4 배열 4 -2 배열의 초기화 및 사용 l 실습예제 Multi. Array. Test 3. java 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 count++; } } } for(i=0; i<three. D. length; i++) { System. out. println((i+1) + "번째 2 차원 배열 "); for(j=0; j<three. D[i]. length; j++) { for(k=0; k<three. D[i][j]. length; k++) System. out. print(three. D[i][j][k] + " "); System. out. println(); } 4장. 자바의 기본 구조 II : 선택, 반복, 배역 122
- Slides: 130