본문 바로가기
풀스택/Java다시 복습 처음부터

java 복습 2(제어문) java if문,for문,switch문,while문,for each문

by woohyun22 2019. 6. 3.

if는 넘어가고


switch문 예시


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package helloworld;
 
public class SwitchDemo {
    public static void main(String[] args) {
        int month = 3;
        String monthString = "";
        switch (month) {
            case 1:  monthString = "1월";
                     break;
            case 2:  monthString = "2월";
                     break;
            default: monthString = "더 만들던가";
                     break;
        }
        System.out.println(monthString);
    }
}
cs


while문 예시


1
2
3
4
5
6
7
8
9
10
11
12
13
14
package helloworld;
 
public class WhileDemo {
    public static void main(String[] args) {
        int treeHit = 0;
        while (treeHit < 10) {
            treeHit++;
            System.out.println("나무를  " + treeHit + "번 찍었습니다.");
            if (treeHit == 10) {
                System.out.println("정말 단단한 나무구나");
            }
        }
    }
}
cs


for, foreach문 예시


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package helloworld;
 
public class SwitchDemo {
    public static void main(String[] args) {
        String[] numbers = {"one""two""three"};
        for(int i=0; i<numbers.length; i++) {
            System.out.println(numbers[i]);//0번쨰부터 number.length까지
        }
        //foreach
        String[] numbers2 = {"one""two""three"};
        for(String number1: numbers2) {//number1부터 numbers2까지
            System.out.println(number1);
        }
    }
}
cs


for,continue


1
2
3
4
5
6
7
int[] marks = {9025674580};
for(int i=0; i<marks.length; i++) {
    if (marks[i] < 60) {
        continue;
    }
    System.out.println((i+1)+"번 학생 축하합니다. 합격입니다.");
}
cs



728x90

댓글