public class Switch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 1;
// a가 1이면 헬로우, 2이면 바이, 3이면 좋아요, 4면 나이스
// 위에 해당 안되면 끝~ 이라고 출력
// switch 조건문
switch(a) {
case 1 :
System.out.println("헬로우");
break;
case 2 :
System.out.println("바이");
break;
case 3:
System.out.println("좋아요");
break;
case 4:
System.out.println("나이스");
break;
default :
System.out.println("끝 ~");
break;
}
}
}
● 만약 a가 1이면 헬로우, 바이, 좋아요 ,나이스가 다 나온다 그래서 밑의 코드를 실행하지 않도록 break문을 써야한다.
public class Loop {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1부터 100까지 출력하시오.
for (int i = 1; i <101; i ++ ) {
System.out.println(i);
}
// 1부터 100까지 값을 다 더하세요
int result = 0;
for (int i = 1; i< 101; i++) {
result += i;
}
System.out.println(result);
// 1부터 100까지의 홀수만 다 더하시오 그리고 그 결과를 출력하시오
int total = 0;
for (int i = 1 ; i < 101; i++) {
if(i % 2 != 0) {
total += i;
}
}
System.out.println(total);
// while 반복문
int i = 1;
while ( i < 101) {
System.out.println(i);
i++; // i = i + 1
}
total = 0;
i = 1;
while (i < 101) {
total += i;
i++;
}
System.out.println(total);
while(true) {
System.out.println("무한루프");
}
}
}
● for( 변수 선언; 조건; 조건에 해당될 때 적용할 부분)
'Java' 카테고리의 다른 글
자바 함수 0630 (0) | 2023.06.30 |
---|---|
자바 배열(Array) 0630 (0) | 2023.06.30 |
자바 연산자 활용, 조건문 0630 (0) | 2023.06.30 |
자바 Project, class 생성 및 문법 기초 0629 (0) | 2023.06.29 |
Eclipse 설치하기 (0) | 2023.06.29 |