티스토리 뷰

1. while - 어떤 작업들이 어떤 조건을 만족할 때까지 반복한다.

while (true) {

break;

}

실습1

1
2
3
4
5
6
7
8
9
10
11
public class WhileTest {
 
    public static void main(String[] args) {
        
        int i = 0;
        while ( i < 10 ) {
            
            System.out.println(i);
            i++;
            
        }
cs

 

실습2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
 
public class WhileTest {
 
    public static void main(String[] args) {
 
        
        Scanner input = new Scanner(System.in);
        String inputValue = "";
        
        boolean isRunning = true;
        while ( isRunning ){
            
            System.out.println("출력 중....종료하려면 exit를 입력하세요.");
            inputValue = input.next();
            
            if (inputValue.equalsIgnoreCase("exit")){//.equals는 대문자를 확실히 구분 .equalsIgnoreCase는 대소문자 구분 안함
                isRunning = false;
            }
        }
        
cs

 

2. 배열 - 같은 타입의 변수를 하나의 집합으로 다루는 것.

하나의 배열에서 여러 개의 값을 관리한다.

기본형 변수가 배열로 선언되면 참조형 변수가 된다. (인스턴스라는 말)

  • 배열의 선언 - 대괄호 ( [ ] )를 사용한다.

데이터형 [] 변수명;

int [] my_array;

  • 배열의 초기화

변수명 = new 데이터형 [요소 수];

my_array = new int[5];

배열은 Primitive Type, Reference Type 관계 없이 new 키워드를 사용한다.

[요소 수] 에는 만들고 싶은 index의 수를 적으며, 추후에 변경이 불가능하다.

  • 배열 데이터 할당, 참조 - 인덱스 번호를 기준

할당 : 변수명 [ 인덱스 ] = 값;

my_array [1] = 10;

참조 : 변수명 [ 인덱스 ];

System.out.println( my_array[1] );

 

실습1 - 배열 선언과 초기화 두 가지 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public class ArrayTest {
 
    public static void main(String[] args) {
        // 첫번째 방법
        // 5개의 문자열이 들어가는 배열 변수를 만든다.
        String list[] = new String[5];
        
        list[0= "김광민";
        list[1= "백지경";
        list[2= "김현섭";
        list[3= "남준호";
        list[4= "황성재";
        
                // 두번째 방법
        int scores[] = new int[] { 102050};
        
cs

 

실습2 - 배열 출력 두 가지 방법

1
2
3
4
5
6
7
8
9
        // 배열 출력 첫번째
                for ( int i = 0; i < 5; i++) {
            System.out.println(list[i]);
        }
        // 배열 출력 두번째
        for ( String name : list ){
            System.out.println(name);
        }//속도가 위에 것보다 더 빠르다
        
cs

 

실습3 - 2차원 배열 출력 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
public class Array2Test {
    public static final int TEAM_COUNT = 5;
    public static final int TEAM_PERSON = 6;
    
    public static void main(String[] args){
        
        String students[][] = new String[TEAM_COUNT][TEAM_PERSON];
        
        students[0][0= "황소리";
        students[0][1= "공정민";
        students[0][2= "구본호";
        students[0][3= "엄기표";
        students[0][4= "조윤후";
        students[0][5= "문석현";
        
        
        students[1][0= "이람";
        students[1][1= "배성진";
        students[1][2= "전민정";
        students[1][3= "조형근";
        students[1][4= "황성재";
        students[1][5= "김장호";
        
        students[2][0= "김현섭";
        students[2][1= "권순길";
        students[2][2= "김동규";
        students[2][3= "김하늘";
        students[2][4= "양지한";
        students[2][5= "남준호";
        
        students[3][0= "김광민";
        students[3][1= "백지경";
        students[3][2= "이기연";
        students[3][3= "조민제";
        students[3][4= "유병훈";
 
        students[4][0= "이승섭";
        students[4][1= "백수경";
        students[4][2= "안신미";
        students[4][3= "오평화";
        students[4][4= "김연";
        
        int teamCount = students.length;
        int studentCount = 0;
        
                // 2차원 배열 출력 첫번째
        for ( int i = 0; i < teamCount; i++ ) {
            System.out.println(students[i].length); 
            //66666 이 나오는 이유 : 5번라인에서 초기값을 6으로 해놨기 때문에 6번 인덱스가 비어있는 상태이지만 길이는 6으로 출력된다.
            studentCount = students[i].length;
            for ( int j = 0; j < studentCount; j++){
                System.out.println(students[i][j]);
            }
        
        }
 
        // 2차원 배열 출력 두번째
        for ( String[] team : students ) {
            forString person : team ) {
                System.out.println(person);                
            }
            
            System.out.println("=========");
        }
    }
}
 
cs

 

 

3. Package를 쓰는 이유

- 관련된 파일 정리(모으기)위해서

- 같은 이름의 클래스를 쓰기 위해서 ( 원래 클래스의 이름이 중복되면 안 된다. )

- 다른 패키지에서 접근 못하도록 막을 수 있다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함