
package chap04;
public class SwitchEx {
public static void main(String[] args) {
int num = (int) (Math.random() * 6) + 1;
switch (num) {
case 1:
System.out.println("1번이 나왔습니다.");
break;
case 2:
System.out.println("2번이 나왔습니다.");
break;
case 3:
System.out.println("3번이 나왔습니다.");
break;
case 4:
System.out.println("4번이 나왔습니다.");
break;
case 5:
System.out.println("5번이 나왔습니다.");
break;
default:
System.out.println("6번이 나왔습니다.");
break;
}
}
}

package chap05;
import java.util.Scanner;
public class check_06 {
public static void main(String[] args) {
boolean run = true;
int studentNum = 0;
int[] scores = null;
Scanner scanner = new Scanner(System.in);
while(run) {
System.out.println("--------------------------------");
System.out.println("1. 학생수 | 2. 점수입력 | 3. 점수 리스트 | 4. 분석 | 5. 종료");
System.out.println("--------------------------------");
System.out.print("선택> ");
int selectNo = Integer.parseInt(scanner.nextLine());
if(selectNo == 1) {
System.out.print("학생수> ");
studentNum = Integer.parseInt(scanner.nextLine());
scores = new int[studentNum];
} else if (selectNo == 2) {
for(int i = 0; i < studentNum; i++) {
System.out.print("scores[" + i + "]> ");
scores[i] = Integer.parseInt(scanner.nextLine());
}
} else if (selectNo == 3) {
for(int i = 0; i < studentNum; i++) {
System.out.println("scores[" + i + "] " + scores[i]);
}
} else if (selectNo == 4) {
int max = 0;
int sum = 0;
for (int s : scores) {
if (s > max) max = s;
sum += s;
}
double avg = (double) sum / studentNum;
System.out.println("최고 점수: " + max);
System.out.println("평균 점수: " + avg);
} else if (selectNo == 5) {
run = false;
}
}
System.out.println("프로그램 종료");
}
}

if( 조건식 ) {
실행문 1
}
실행문 2
if( 조건식 ) {
실행문 1
} else {
실행문 2
}
실행문 3
if (조건식 1) {
실행문 1
} else if (조건식 2) {
실행문 2
} else {
실행문 3
}
실행문 4
💡조건문은 {}괄호 블럭이 어디에서 시작하고 어디서 끝나는지 반드시 알아야함 실수 하기 매우 쉽다
→ 조건식에 참,거짓을 판별하는 것이 아닌, 변수가 어떤 값을 갖느냐에 따라 실행문이 결정
switch(변수) {
case 값1:
실행문 1
break;
case 값2:
실행문 2
break;
default:
실행문 3
}
💡만약 변수가 값2이고 break문이 없으면 실행문 2 실행 후 실행문 3도 실행 해야한다
package chap04;
public class forEx {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i<=100; i++) {
sum = sum + i;
}
System.out.println("1~100의 합 " + sum);
}
}
📝 1부터 100까지 정수를 하나씩 더해서 sum에 누적
package chap04;
public class whileEx {
public static void main(String[] args) {
int i = 1;
while(i<=10) {
System.out.println(i);
i++;
}
}
}
package chap04;
public class dowhileEx {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i); //실행문 1
i++; //실행문 2
} while(i<=10);
}
}
💡 break문 과 continue문
package chap05;
public class ArrayEx {
public static void main(String[] args) {
int a[][] = {{1,2,3,4,5},{6,7}};
System.out.println(a[0].length);
System.out.println(a[1].length);
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][1]);
}
}
a[][]를 다차원 배열로 구성
1 2 3 4 5
6 7
로 저장
a[0].length → a[0]행의 배열의 길이 5
a[1].length → a[1]행의 배열의 길이 2
a[0][0] → 1
a[0][1] → 2
a[1][1] → 7
package chap05;
public class RefArrayEx {
public static void main(String[] args) {
String[] strArray = new String[3];
strArray[0] = "abcd";
strArray[1] = "abcd";
strArray[2] = new String("abcd");
System.out.println(strArray[0] == strArray[1]);
System.out.println(strArray[0] == strArray[2]);
System.out.println(strArray[0].equals(strArray[2]));
}
}
결과는 true false true