본문 바로가기

카테고리 없음

배열과 for-each 문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package day_06;
public class Day06Ex06 {
    
 
    public static void main(String[] args) {
        
        int[] num= {1,2,3,4,5};
        int sum= 0;
        for (int k:num) //반복될 때마다 k는 num[0], num[1], . . . num[4]값으로 설정
            sum += k;
        System.out.println("합은" + sum);
        
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

합은 15

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package day_06;
public class Day06Ex06 {
    
 
    public static void main(String[] args) {
        
        /*
        int[] num= {1,2,3,4,5};
        int sum= 0;
        for (int k:num) //반복될 때마다 k는 num[0], num[1], . . . num[4]값으로 설정
            sum += k;
        System.out.println("합은" + sum);
        */
        
        String[] names= {"사과","배","바나나","체리","딸기","포도"};
        for(String s :names) // 반복할 때마다 s는 names[0], names[1] . . . .names[5] 로 설정
            System.out.print(s + " "); 
        
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

사과 배 바나나 체리 딸기 포도 

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package day_06;
import java.util.Scanner;
public class Day06_Homework {
    
 
    public static Scanner scanner= new Scanner(System.in);
    
    public static void main(String[] args) {
        int[] days= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month= 0;
        int day= 0;
        int total= 0;
        
        // total에 날짜수(days)를 다 모아서 1년은 365일 입니다. 출력하기
        for(int i : days ) {
            total+= i;
                    
        }
        System.out.printf("1년은 %d달입니다.\n", days.length);
        System.out.printf("1년은 %d일입니다. \n", total);
        
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

1년은 365입니다.