출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

1
2
3
4
5
6
7
8
9
10
11
12
13
            int n=0;
            
            for(int j=00 ; j<9 ; j++) {
                
                if(j<5
                    n++;
                else n--;
                
                for (int i =0; i<n; i++) {
                    System.out.print("*");
                }
                System.out.println();
            }
cs

참고 링크  : pridiot.tistory.com/24

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        char arr[] = {'D''W''O''A''B'};
        // 모두 소문자로 변경한 후 출력하라.
        // A=65, a=97 -> 32 
        // System.out.println("ASCII = " + (int)arr[0]);  알파벳을 ASCII 코드로변경해서 출력해보기
        
        int asc = (int)arr[0];    // D의 ASCII코드값 얻기
        asc = asc+32;            // 소문자로 변환        
        char ch = (char)asc;    // 문자로변환
        System.out.println(ch); // 출력
        
        for(int i=0 ; i<arr.length ; i++) {
            char c = arr[i]; // char c에 순서대로 알파벳 넣기
            int asc2 = (int)c; // arr[i]값의 ASCII 코드값 얻기
            asc2 = asc2+32// 소문자로 변환하기
            arr[i] = (char) asc2; // 다시 arr[i]에 소문자로 바뀐 값 넣어주기
            System.out.print(arr[i]);
            
        }
cs
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
char arrch[] = {'D''W''O''A','B'}; 
// A를 찾아서 a로 변경하고 출력하라 
 
 
//for문으로 
        for (int i=0 ; i < arrch.length ; i++) { 
            if(arrch[i]=='A') { 
                arrch[i]='a'
                System.out.print(arrch[i]);}
            else { 
                System.out.print(arrch[i]);} // DWOaB로 출력
    } 
 
 
System.out.println(""); 
 
//foreach문으로
        
        char arrch1[] = {'D''W''O''A','B'};
        
        
        int index=0 ; // 따로 변수를 줌
        
        for(char c : arrch1) {
            if(c == 'A') {
                arrch1[index] = 'a';
            }
            index++;
        }
        
        for(int i=0; i<arrch1.length;i++) {
            System.out.print(arrch1[i]);
        }
        
 
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        // 1~100 사이에 있는 숫자 중 짝수들의 합, 홀수들의 합을 구하라.
        
        int sum1=0//짝수합
        int sum2=0//홀수합
        
         for(int i=1 ; 101>i; i++) {
             if(i%2==0) {
                 sum1=sum1+i;
             }
             else {
                 sum2=sum2+i;
             }             
         }
         
         System.out.println("짝수합 = " + sum1);
         System.out.println("홀수합 = " + sum2);
cs

for(int i=0; i<Array.size(); i++){ }

 

 //일반 for문

 

for(int num : Array){ }

 

//범위 기반 for문(foreach)

 

 

 

 

 

1
2
3
4
5
6
        
        int array[]= {12,13,14,15};
        
        
        for(int num:array) {
            System.out.println(num);
cs

 

12

13

14

15 

 

로 출력된다.

 

 

 

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
        // for-each구문 : 배열의 원본 데이터의 수정이 아닌 일괄 참조의 목적일 때 사용할 수 있다.
        
        int[] arr4 = {1,2,3,4,5};
        
        
        for(int a:arr4) {
            System.out.print(a +" "); // 1 2 3 4 5 
        }
        
        
        System.out.println(); // 엔터
        
        
        int sum=0;
        for (int a:arr4) {
            sum+=a;
        }
        
        System.out.println("배열 arr4의 요소들의 합 " + sum); //15
        
        // for - each에서의 값의 변경 > 배열에는 영향을 주지 않는다.
        for(int a:arr4) {
            a++;
            System.out.print(a+" ");  // 2 3 4 5 6  (변수 a를 증가시켜 출력만 이렇게나온것)
            
        }
        
        System.out.println(); // 엔터
        
        for(int a:arr4) {
            System.out.print(a+" "); // 1 2 3 4 5  (원본 그대로 나옴)
        }
cs

'JAVA > basic' 카테고리의 다른 글

Break 문  (0) 2020.09.24
Loop 문 ( Do while 문 )  (0) 2020.09.24
Loop문 ( while 문 )  (0) 2020.09.24
Loop문 ( for문 )  (0) 2020.09.23
스위치 문 ( Switch 문 )  (0) 2020.09.23
조건문 ( if, if-else, else if )  (0) 2020.09.23
배열 (array) : 배열 초기화, 배열 swap, 배열 별명  (0) 2020.09.23
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
69
        /*
         순환문 : loop문
         조건이 만족하면 반복 실행되는 제어문
         
         for, foreach
         
         */
        
    /* for문 형식
     
         for( 변수선언 또는 초기화 ; 조건문 ; 연산식 ){
         처리}
         연산식에는  increment++을 제일 많이 사용한다.
         
         for (변수선언 : 배열 or 목록){
         처리}
     
     */
        
        // int = a | 외부에서 선언 or 안에서 선언(초기화불가능/재사용가능)
        
        
        //예제1)
        
        for (int a=0 ; a<4 ; a++) { // a=0에서 시작해서 4보다 클때까지 ++1
            System.out.print("1");
        }
        
        System.out.println();
        
        //예제2)
        
        int sum = 0;
        
        for(int i=1 ; i<=10 ; i++) {
                sum = sum+i;}
        
        System.out.println(sum);
        
        // 예제3) 배열의 길이
        
        int array[] = {23,24,35};
        System.out.println(array.length); // 배열.length
        
        sum=0;
        // sum = array[0] + array[1] + array[2]
        
        for (int i=0 ; i<array.length ;i++) {
            sum = sum+array[i];
        }
            
        System.out.println(sum);  // 82
 
        // 예제4) for문 안의 if문 _ 양수만 더하기
        
        int forif[]= { 10-2030 };
        
        sum=0;
        
        for (int i=0 ; i<forif.length;i++) {
            if(forif[i]>0) {
                sum=sum+forif[i];
            }
        }
        
        System.out.println(sum);   // 10+30=40 
        
        
        
cs

(범위설정) 부등호는 사용할 수 없다.

숫자가 명확야 한다.

소숫점(실수값)은 사용할 수 없다. (ex. double)

- char나 Spring은 사용 가능 

 

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
        /* 
         
        Switch(변수){
            case 값1:
            처리1
            break;
            case 값2;
            처리2
            break;
            :
            default:          // else와 같은 역할 없어도됨
                처리
        }
        
        
        */
        
        int number = 3;
        
        switch(number) {
            case 0:
                System.out.println("number = 0");
                break;
            case 1:
                System.out.println("number = 1");
                break;
            case 3:
                System.out.println("number = 3");  // 출력
                break;
        }
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
    String str1 = "hello";
    String str2 = "hell";
    
    str2 = str2 + "o";
    System.out.println(str2); // hello로 출력
    
    if (str1==str2) {
        System.out.println("같다");  // 같다고 인식하지 않음
    }
    
    if (str1.equals(str2)) {
        System.out.println("같습니다"); // 같다고 인식함
    }
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        Scanner sc = new Scanner(System.in);
        
        int a;
        
        System.out.print("숫자를 입력하세요 : ");
        a=sc.nextInt();
        
    
        
        if (a%2==0) {
            System.out.println("짝수입니다");
            }
        else {
            System.out.println("홀수입니다");
        }
cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
        /* 
    
         - 제어문
             
             조건문
                 if
                 else
                if - else if - else
                switch
                 
             순환문
                 for
                 foreach (for문을 간단하게 만든 것)
                 while
                 do while
                 /break
                 /continue
             
         */
        
                    
            
    //    if ( 조건(true) ) { 처리  }
        // if 예제1)
        
        int number=5;
        
        if (number>0) { //참 
            System.out.println("number는 0보다 큽니다"); // 출력
        }
        if (number>5) { //거짓
            System.out.println("number는 5보다 큽니다"); //출력되지않음
        }
        
        // if 예제2)
        
        boolean b=true;
        if(b==true) {
            System.out.println("B는 TRUE입니다");
        if(b!=false)
            System.out.println("B는 TRUE입니다");
        }
        
        
        
    /* if(조건1=true){실행}
         else{실행}                  // 조건1이 false일 경우
     */
        
        
        
        // if-else 예제1
        
        number=10;
        if(number>5) {
            System.out.println("넘버는 5보다 크다");} // 출력O
        else {
            System.out.println("넘버는 5보다 작다");} // 출력X
        
            
    /* 조건분기 n개의 조건
         if
         else if
         else if * n개
         else
     
     */
    
        
        // else if 예제1
    
        number = 85 ;
        
        if(number==100) {
            System.out.println("A");}
            else if (number >=80 ) {
            System.out.println("B");}
            else if (number >=70 ) {             // B 출력
            System.out.println("C");}
            else
            {System.out.println("D");}
            
    
    
        // 조건문 안에 조건문
    
        number=95;
        
        if(number>=90) {
            if(number>=95) {
                System.out.println("A++");
            }
            else {System.out.println("A");}
        }
        else if (number>=80) {
            
        }
        
    
    
 
cs

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        
        /*  Array(배열) : 같은 자료형 변수들의 묶음.
         형식 :    자료형    배열변수명[] = new 자료형[배열의총수]
                int    arr[]= new int[5]
                int[] arr= new int[3]
                int []arr = new int[3]
        
            arr[index number] = 값
            arr[0]~arr[4] 
            
            초기화 int arr[]=null;
        */
        // 5명 학생의 국어점수 총점, 평균 구하기
        
        int lang1, lang2, lang3, lang4, lang5;
        
        lang1=90;
        lang2=85;
        lang3=100;
        lang4=95;
        lang5=75;
        
        int sum = 0 ;
        
        sum= lang1 + lang2 + lang3 + lang4 + lang5;
        
        double avg=0;
        
        avg =sum/5;
        
        // 배열로 구해보기
        
        int arrlang[] = new int[5]; 
 
        /* int arrlang[];
         arrlang = new int[5]; */
        
        arrlang[0= 90;
        arrlang[1= 85;
        arrlang[2= 100;
        arrlang[3= 95;
        arrlang[4= 75;
        
        
        System.out.println(arrlang[3]);
        
        
    //배열초기화
        int arrnum[]= {9010085};
                
        // char arrchar[] = { 'A','B','C'}; 선언과동시에 초기화하기
        
        char arrchar[];
        // arrchar = { 'A','B','C'} 불가능
        
        arrchar=new char[3]; // 동적할당을 한 이후에 초기화 가능, 선언과 동시에 초기화해야함
        arrchar[0]='A';
 
                
    //alias(별명)
                
            int array[]= {11,22,33};
            int alias[]=array;
            
            System.out.println(alias[2]); // =33
            
            alias[2]=222;
            System.out.println(alias[2]); // =222
            System.out.println(array[2]); // =222
            
    //swap 
            
            int a[] = {1,2,3};
            int b[] = {4,5,6};
            int temp;
            
            /*temp=a[0];
            a[0]=b[0];
            b[0]=temp; */ 
            
            int aliA[]=a;
            int aliB[]=b;
            int alitemp[];
            
            alitemp=aliA;
            aliA=aliB;
            aliB=alitemp;
            
            System.out.println(aliB[0]);
    
cs
ex) float + int = float + float

 

* byte + byte = int + int -> int

 

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
        /*
                                             우선순위
                                            
            boolean        1byte                낮음
                                          
           byte        1byte              
           short        2byte            
           int            4byte              
           long        8byte                   
                                           
           float        4byte               
            double        8byte                높음
            
            자료형의 변환 (auto, cast)
            
         */
        
        
        
        // 자동 자료형 변환 (auto)
        
        short sh = 12345;    // 2byte
        int i;                // 4byte
        
        i = sh; //자동형변환
        System.out.println("i = " + i);
        
        
        
        
        // 강제형 변환 (cast)
        
        i = 1234567891;
        
        // sh = i; 에러남!
        
        sh = (short) i; 
        System.out.println("sh = "+ sh);  
        // sh=723이 나온다.
        // 그러므로 형변환 할 때는 범위의 값을 잘 생각해서 변환해야한다.
        
        
        
        
        
        long l = 123456789L;
        float f = l;
        double d = l;
        
        System.out.println("f = " + f); // float라서 용량부족으로 여섯번째 숫자부터 에러남
        System.out.println("d = " + d); // 1.2345678E8
        
        //1.23456789E8
        //1.23456789 * 10^8
        
        double d1 = 1.234e2// 123.4 
        double d2 = 1.234e-2// 0.01234
        
        int n1, n2;
        n1=3;
        n2=2;
        
        double result = n1 / n2 ;  // 1.5가 나오지 않는다.
        double result2 = (double)n1/n2; 
        
        System.out.println(result); // 1.0 틀림
        System.out.println(result2); // 1.5 맞음
        
cs
 
 
 
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
         int a; // =3240; // 지불해야할 금액
         int b; //=10000; // 본인이 낸 금액
         
         Scanner money=new Scanner(System.in);
         System.out.print("지불해야할 금액 : ");
         a=money.nextInt();
         System.out.print("낸 금액 : ");
         b=money.nextInt();
         
         
         int c; // 거스름돈
         c=b-a; 
    
         int d,e,f,g,h,i; // 5000원 1000원 500원 100원 50원 10원의 갯수
         
         
         
        /* d=c/5000; // 5000원권의 갯수
         e=(c-(d*5000))/1000; // 1000원권의 갯수
         f=(c-(d*5000)-(e*1000))/500 ;// 500원 갯수
         g=(c-(d*5000)-(e*1000)-(f*500))/100 ; // 100원 갯수
         h=(c-(d*5000)-(e*1000)-(f*500)-(g*100))/50; // 50원 갯수
         i=(c-(d*5000)-(e*1000)-(f*500)-(g*100)-(h*50))/10; // 10원 갯수 */
         
         d=c/5000;
         e=(c%5000)/1000;
         f=(c%1000)/500;
         g=(c%500)/100;
         h=(c%100)/50;
         i=(c%50)/10;
         
         
         System.out.println("5000원 갯수 = " + d);
         System.out.println("1000원 갯수 = " + e);
         System.out.println("500원 갯수 = " + f);
         System.out.println("100원 갯수 = " + g);
         System.out.println("50원 갯수 = " + h);
         System.out.println("10원 갯수 = " + i); 
    
         
cs

//삼항 연산자
// (조건)?"값1":"값2";  
// 조건이 참이면 값1을, 거짓이면 값2를 대입한다

int number = 0 ;
int i;

i= (number<5)?100:200;                  = 100

 

String str;

str= (number>0)? "양수입니다":"0이거나음수입니다"               = 0이거나음수입니다

 

 

 

 

&&

AND 그리고, 두가지가 모두 참인경우 -> true


  ||

OR 또는, 두가지 중에 하나만 참이거나 둘 다 참인 경우 -> true


  ! 

~아닌,NOT

>,<,==

크고 작고 같고~
>=, <=

크거나 같고 작거나같고~ 

a != b

a와 b가 같지않다

 

!(num<0) → num>=0

!(num>0 || num<10)) num<=0 && num>=10

 

+ Recent posts