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
class  Method
{
    //int n;
    public static void main(String[] args) 
    {
 
        //지역변수, Local Varibale
        //  - 메소드나 제어문안에서 선언된 변수
        //int n;                 가 선언된 영역을 말함
 
        //지역 변수의 생명주기, Life Cycle
        //  - 영역, Scope
        //  - 변수가 메모리에 언제 생성 ~ 소멸
        //  - 변수 선언문이 실행되는 순간 메모리에 할당
 
        int n;    // ->메모리에 생성되는 순간
        n = 10;
        System.out.println(n);
 
        test();
        //System.out.println(m); x
 
 
    }//main
 
    public static void test()
    {
        int n;
        int m;
        m = 20;
        System.out.println(m);
        //System.out.println(n); x
    }
 
 
}//class
 
cs


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

메소드5 (재귀 메소드)  (0) 2015.05.22
메소드4 (메소드 오버로딩)  (0) 2015.05.22
메소드2 (구성요소)  (0) 2015.05.22
메소드1  (0) 2015.05.22
연산자  (0) 2015.05.22
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
class Method 
{
    //main() 메소드는 프로그램 실행시 자동으로 호출됨
    public static void main(String[] args) 
    {
 
        //메소드 구성요소 
        //두가지를 사용하면 가용도 높은 메소드 가능
        //1. 인자값
        //2. 반환값
 
 
        //인자, 매개변수, 파라미터(Parameter).,
 
        //요구사항] 홍길동에게 인사 -> 메소드 선언
        //수정사항] 아무게에게도., 
        //수정사항] 우리 교실의 모든 사람마다..
        hello();
        hello2();
        
        //에러 : no arguments  -> 변수가 선언돼있는 상태이므로 괄호(name)를 채워야 한다.
        //hi();
 
        hi("홍길동");
        hi("아무게");
 
 
        //10 + 5
        // 3 + 6
        sum(105);
        sum(36);
 
        //에러 type
        ///hi(100);
        //sum(10); //에러 : 10+null이므로 에러
        //sum(10, 20, 30);
        
        //** 인자값 사용 시 주의점 **
        //1. 인자의 자료형 일치
        //2. 인자의 갯수 일치
 
 
 
 
        //반환
        int n = getNum();
        System.out.println(n);
 
        String name = getName("홍길동");
        System.out.println(name);
    }
 
 
 
 
    //1. 인자 ex
    public static void sum(int a, int b)
    {
        System.out.printf("%s + %d = %d\n", a, b, a + b);
    }
 
 
 
 
    public static void hi(String name) // (String name = 홍길동) -> to불특정인물
    {
        //String name;
        //name = "홍길동"; //x
        System.out.printf("%s님 안녕하세요~\n", name);
    }
 
 
 
 
    public static void hello()  // -> 이런 st.는 고지식한 메소드, 유연하지 못함.. to특정인물
    {
        System.out.println("홍길동님 안녕하세요~");
    }
    
    //에러 : method hello() is already defined in class
    public static void hello2()
    {
        System.out.println("아무게님 안녕하세요~");
    }
 
    public static void test()                
    {
       System.out.println("테스트");
    }
 
 
 
 
    //2.반환 ex
    public static int getNum()
    {
        int n = 10;
        
        //리턴문
        //  - 메소드의 실행을 끝내고 호출했던 곳으로 돌아가라
        //  - *** 뒤의 데이터를 가지고 돌아가라 ***
        return n;
    }
 
 
    //시그너쳐의 반환타입과 return문의 타입 다르면 에러
    public static String getName(String name)
    {
        String temp = name + "님";
        return temp;
        
        //두개 이상의 값을 반환하지는 못한다.
        //return temp, temp2;
    }
 
 
 
 
 
 
}
cs


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

메소드4 (메소드 오버로딩)  (0) 2015.05.22
메소드3 (지역변수)  (0) 2015.05.22
메소드1  (0) 2015.05.22
연산자  (0) 2015.05.22
BUFFEREDREADER  (0) 2015.05.21
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
 
class Method 
{
    //메인 메소드
    //  - 예약된 메소드
    //  - 프로그램의 시작점(Start Point) - 종착점(End Point)
    public static void main(String[] args) 
    {
        //Ex14_Method.java
 
        //메소드, Method
        //  - 코드의 집합
        //  - 특정 행동을 목적으로 하는 코드의 집합
        //  - 행동(Behavior), 동적인 성격
        //  - 반복되는 코드의 집합(코드 재사용)
        //  - 쪼갤 수 있는 한 쪼개서 사용!
 
        //메소드 사용 시 장점
        //1. 수정하기 쉽다(유지보수성 높음)
        //2. 코드를 성격에 따라 분리(가독성 높음)
        //3. 코드의 재사용 높음
 
 
        //1. 메소드 선언(구현, 정의) : 1회
        //2. 메소드 호출(사용) : n회
 
        //요구사항] "반갑습니다~" x5회
        //수정사항] "반갑습니다~""
        System.out.println("반갑습니다~");
        System.out.println("반갑습니다~");
        System.out.println("반갑습니다~");
        System.out.println("반갑습니다~");
        System.out.println("반갑습니다~");  // ->복사/붙여놓기 : 막코딩, 하드코딩st 자제한다. -> 메소드로 처리해야
 
 
        //메소드 호출
        hello();
        hello();
        hello();
        hello();
        hello();
 
        number(); number(); number(); number(); number();
        number(); number(); number(); number(); number();
        //하나~열 출력 x10회
        System.out.println("하나");
        System.out.println("둘");
        System.out.println("셋");
        System.out.println("넷");
        System.out.println("다섯");
        System.out.println("여섯");
        System.out.println("일곱");
        System.out.println("여덟");
        System.out.println("아홉");
        System.out.println("열");  // -> 너무 길어진다..
 
 
 
    }//main (메소드 안에는 메소드를 만들지 못함)
     
     //메소드 선언
     // ->메소드 시그너처(Signature), 함수 원형(Prototype)
     //접근지정자 [정적키워드] 반환타입 메소드명([인자리스트]) -> 머리, 메소드 시그너쳐
     // public      static       void     main (String[] args)
     //{                                                       -> 메소드 본문(Body, 구현부)
     //  실행코드;
     //}
 
     public static void hello()
    {
        //반복해서 실행할 코드 작성
        System.out.println("안녕하세요!!"); // 반복해서 넣을 단위를 하나 작성
    }
 
    
    public static void number()
    {
        System.out.println("하나");
        System.out.println("둘");
        System.out.println("셋");
        System.out.println("넷");
        System.out.println("다섯");
        System.out.println("여섯");
        System.out.println("일곱");
        System.out.println("여덟");
        System.out.println("아홉");
        System.out.println("열");
    }
 
}//class
 
cs


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

메소드3 (지역변수)  (0) 2015.05.22
메소드2 (구성요소)  (0) 2015.05.22
연산자  (0) 2015.05.22
BUFFEREDREADER  (0) 2015.05.21
INPUT(입력)  (0) 2015.05.21
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
 
class op 
{
    public static void main(String[] args) 
    {
 
        //연산자, Operator
        //  - 피연산자를 사용해서 (미리 정해진)연산을 한 수에 결과값을 반환하는 역할
 
 
 
 
 
 
        //1. 산술 연산자
        //  - +, -, *, /, %(mod, 나머지)
        //  - 이항 연산자(2항) : 피연산자의 갯수
        
        //int result = 1 + 2 x 3 - 4; //문장, 1개이상의 표현식
 
        int a = 10;
        int b = 3;
 
        System.out.println(a + b); //13
        System.out.println(a - b); // 7
        System.out.println(a * b); //30
 
        //피연산자 중 더 큰자료형으로 반환한다.
        //정수 / 정수 = 정수(반올림x, 소수이하는 버림)
        //실수 / 실수 = 실수
        //정수 / 실수 = 실수
        //실수 / 실수 = 실수
        System.out.println(a / (double)b); // 3, 정수int/정수int로 했기때문에 나머지출력x -> 보고싶다면 (double)입력
        System.out.println(a / 3); //반올림x, 소수이하 버림**
        System.out.println((double)a / 3);
        System.out.println(a / 3.0); //***
        System.out.println(a % b); // 1
 
 
 
 
 
 
        //2. 비교 연산자
        //  - 피연산자들의 비교 우위를 반환하는 역할
        //  - >, >=, <, <=, ==(equal), !=(not equal)
        //  - 연산의 결과를 항상 boolean값(true/false)으로 반환
        //  - 이항 연산자
 
        System.out.println(a > b);
        System.out.println(a >= b);
        System.out.println(a < b);
        System.out.println(a <= b);
        System.out.println(a == b); //*갯수주의
        System.out.println(a != b);
 
        //(****)문자열 비교(****)
        String str1= "홍길동";
        String str2= "홍길동";
        String str3= "홀";
        str3 = str3 + "길동"//"홍길동"
        
        int n1 = 10;
        int n2  = 10;
        int n3 = 2 * 5;
        
        System.out.println(n1 == n2);
        System.out.println(str1 == str2);
 
        System.out.println(n1 == n3);
        System.out.println(str1 == str3);
        System.out.println(str1);  //*
        System.out.println(str3);  //*
 
        //문자열 상수(변수)의 비교는 ==(equal) 사용을 하면 안됨!! *****중요!!
        //  -> equals() 메소드를 사용해야
        System.out.println(str1.equals(str2)); //str1 == str2
        System.out.println(str1.equals(str3)); //str1 == str3
 
 
 
 
 
 
        //3.논리 연산자
        //  - 각각의 논리 연산 행동에 따른 결과값 반환
        //  - &&(and), ||(or), !(not)
        //  - 피연산자를 boolean값을 가지고 자신의 연산 결과도 boolean값을 반환한다. 
        
        //and, 좌우측 중 한쪽만 불만족시켜주면 F
        // A && B
        // T && T = T
        // T && F = F
        // F && T = F
        // F && F = F
        // T && T && T && T = T
 
        //or, 좌우측 중 한쪽만 만족시켜주면 T
        // A || B
        // T || T = T
        // T || F = T
        // F || T = T
        // F || F = F  
        // T || F || F || F = T
 
        boolean b1 = true;
        boolean b2 = false;
 
        System.out.println(b1 && b2);
        System.out.println(b1 || b2);
 
 
        //요구사항] 입력받은 나이가 20세이상 60세미만?
        int age = 25;
        
        //비교연산자 > 논리연산자 (비교연산자가 논리연산자보다 먼저 계산됨) **
        //System.out.println(20 <= age < 60); 하면,
        //20 <= age < 60 
        // treu     < 60 로 출력되기 때문에 쪼개야함..
 
        //System.out.println(20 <= age && age < 60);
        //-> System.out.println(true && true);
        System.out.println((20 <= age) && (age < 60));
 
 
        //빨강, 노랑, 파랑 -> 통과
        //그외의 색상 -> 거절
        String color = "검정";
 
        //System.out.println(color == "빨강" || color="노랑" || color="파랑");
        System.out.println(color.equals("빨강"|| color.equals("노랑"|| color.equals("파랑"));
    
        
        //! :단항 연산자(1항 연산자)
        boolean flag = true;
        System.out.println(!flag);
        
 
 
 
 
        //4. 대입(할당) 연산자
        //  - =, +=,-=, *=, /=, %=
        //  - LValue(공간,변수) = RVlaue(값,변수,상수)
        //  - LVlaue와 RValue의 자료형은 일치해야 한다. !! ***
        //  - 연산자 우선 순위 가장 낮음
 
        // += : 복합 대입 연산자
        int n = 10;
 
        n = n + 1//누적, 출력-> n=11;
        n += 1;  //위에 있는 값을 줄인 표현 (n=n+1;)
        
        System.out.println(n); //12
 
        n -= 1// n = n - 1;
        System.out.println(n); //11
        
        n *= 3// n = n * 3;
        System.out.println(n); //33
    
        n /= 4// n = n / 4;
        System.out.println(n); //8
    
        n %= 3// n = n % 3;
        System.out.println(n); //2
 
        //대입 연산자는 '우측 -> 좌측'으로 진행됨 *
        int o, p, q, r;
        o = p = q = r = 10;
 
 
 
 
 
        //5. 증감 연산자
        //  - ++(증가), --(감소)
        //  - 기존의 값을 +1, -1 해라
        //  - 단항 연산자
        //  - 연산자 우선 순위 최상위
        //  - 피연산자의 위치가 변함 ***
        //        a. ++n : 접두어(Prefix) -> 우선순위 최상
        //        b. n++ : 접미어(Postfix) -> 우선순위 최하
 
        n = 10;
         
        //n = n + 1;
        //n + = 1;
        ++n; //누적, n값에 1을 더해 덮어써라
        System.out.println(n); //11
 
        //n = n -1;
        //n -= 1;
        --n;
        System.out.println(n); //10
 
    
        //n++; //자기자신에게 1을 더한다
        //System.out.println(n); //11
 
        n = 10;
        int result = 0;
 
        //증감 연산자는 한문장에 다른 연산자와 같이 쓰지 말것
        //문장1개에 증감 연산자1개를 쓴다
        
        
        //result = 10 + n;
        //n++;
 
        //result = 10 + ++n;    
        //       3    2  1  -> 우선순위
        //               n
        //             21
        System.out.println(result); // 21
 
 
        result = 10 + n++;    
        //     2    1  3  
        System.out.println(result); // 21
        System.out.println(n);      // 12
 
 
        //int o = 10;
        //System.out.println(--o - o--);
 
    
    
 
        //6. 조건 연산자
        //  - A ? B : C
        //  - 삼항 연산자 (3항 연산자)
        //  - A라는 조건식(boolean값 변환)을 만족하면 연산의 결과로 B를 반환하고 만족하지 못하면 C를 반환한다.
        //  - if문
 
        age = 25;
 
        //성년? 미성년?
        String result2 = (age >= 19) ? "성년" : "미성년"//피연산자 A=19, 출력-> 만족하는 값이므로 "성년"
        System.out.printf("나이 %d살은 %s입니다.\n", age, result2);
 
 
        int m1 = 10;
        int m2 = 50;
        int max = 0;
 
        max = m1 > m2 ? m1 : m2;
        System.out.println(max);
 
 
 
 
        //연산자 우선 순위
        // ()소괄호 > 증감(prefix) -> 산술 
        //            > 비교 > 논리 > 대입 > 증감(postfix)
 
        //한문장 내에서 동일한 수준의 연산자들간의 우선순위
        //1. 대부분 : 좌 -> 우
        //2. 대입, 증감 : 우 -> 좌
 
 
        //숏 서킷 룰(short curcuit rule)
        //  - && 연산은 왼쪽이 false이면 오른쪽을 실행안함
        //  - ||       "        true            "
        
        //age = 5;
        //(age > 10) && (age < 50)
 
    }
}
 
cs


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

메소드2 (구성요소)  (0) 2015.05.22
메소드1  (0) 2015.05.22
BUFFEREDREADER  (0) 2015.05.21
INPUT(입력)  (0) 2015.05.21
SCANNER  (0) 2015.05.21
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
 
//패키지 가져오기
//import java.io.BufferedReader; //클래스 가져오기
//import java.io.InputStreamReader; 
import java.io.*;  //패키지 모두 가져오기
//import java.lang.*; //기본 패키지
 
class BufferedReader 
{
    public static void main(String[] args) throws Exception
    {
 
        //요구사항] 이름을 입력받아 인사를 하시오.
        int n = 10;
        //에러 : cannot find symbol
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //reader객체
 
 
        System.out.printf("이릅 입력 : ");
        //입력한 한줄을 가져오기(줄단위로)
        String name = reader.readLine();
 
        System.out.printf("안녕하세요 ~%s님\n", name);
 
 
 
 
        //숫자를 입력받아. 제곱값을 구하시오.
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.printf("숫자 입력 : ");
 
        //사용자가 입력한 내용을 문자열로 반환한다.
        //  - 홍길동 -> "홍길동"
        //  - 20 -> "20"
        //  - true - "true"
        String input = reader.readLine();  //"5"
        
        //System.out.print(input * input);
 
 
        //(유사)형변환**
        //"5" -> 5 
        //String -> int (문자열을 숫자로 바꾸고 싶다면)
        //int num = Integer.parseInt(input);
 
        //String -> byte
        //byte num = Byte.parseByte(input);
    
        //string -> short
        //short num = Short.parseShort(input);
        
        //boolean flag = Boolean.parseBoolean("true");
 
        System.out.printf("%d ^ 2 = %d\n", num , num * num);
 
        //기본 자료형 8가지 중 7가지
        //  0000.parse000("데이터")
 
        //"홍"->'홍'
        String txt = "홍"
        char c = txt.charAt(0);
        System.out.printf("문자 : %c\n", c);
 
 
 
 
        //숫자 2개 입력 -> 산술연산 -> 연산과정 + 결과
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 
        int a = 0, b = 0
        String i1 = "", i2 = "";
        System.out.print("첫번째 숫자 : ");
        i1 = reader. readLine(); // "5"
 
        System.out.print("두번째 숫자 : ");
        i2 = reader.readLine(); // "3"
 
        a = Integer.parseInt(i1); // "5" -> 5
        b = Integer.parseInt(i2); // "3" -> 3
 
        //업무
        System.out.printf("%d + %d = %2d\n", a, b, a + b);
        System.out.printf("%d - %d = %2d\n", a, b, a - b);
        System.out.printf("%d * %d = %2d\n", a, b, a * b);
        System.out.printf("%d / %d = %2d\n", a, b, a / b); // 사이에 2입력, 우측정렬 됨
 
    }
}
 
cs


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

메소드1  (0) 2015.05.22
연산자  (0) 2015.05.22
INPUT(입력)  (0) 2015.05.21
SCANNER  (0) 2015.05.21
OUTPUT(출력)  (0) 2015.05.21
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
 
class Input 
{
    public static void main(String[] args) throws Exception
    {
 
        //콘솔입력
        //  - 사전 작업 2가지
        //     a. throws Excetion 입력
        //       b. 패키지ㅣ 임포트 
        //1. System.in.need();  /  System.out.prinf();   1
        //  - 바이트 입력(한글 입력x)
        //2. BufferedReader  클래스                         1
        //
        //3. Scanner 클래스                                 ▼ ver.up/ 2,3번 주로 사용
 
        // -> 연산자 -> 메소드 -> 제어문
 
        
        //사용자로투버 문자를 받아.. 그대로 출력하시오.
        
        //풀이 : 사용자로부터 문자(무언가)열을 입력받겠습니다..
        // -> 블럭 걸린다. 입력대기상태.
        System.out.print("문자를 입력 :");
    
        
 
        //다중
 
        //String str = System.in.read();  // 실행한 코드는 사라지고 입력한 데이터 " ";가 남게됨.
        //comm : '버퍼' 입력한 데이터를 쌓아두는 공간, enter도 입력됨(\r\n, 두칸)
        int n = System.in.read();
        System.out.println((char)n);  //출력:입력한 문자의 문자 코드값, 1set=한글자
 
        //error: variable n is already defined in method main
        n = System.in.read();
        System.out.println((char)n); //set2
 
        n = System.in.read();
        System.out.println((char)n); //set3
        
        n = System.in.read();
        System.out.println((char)n); //set4
 
 
        /*
        String str = "";
        int n = System.in.read();
        str = aa
            "" + (char)n;
        n = System.in.read();
        str = str + (char)n;
        n = System.in.read();
        str = str + (char)n;
        System.out.printf("사용자가 입력한 데이터 : %s\n", str);
        */
 
    }
}
 
cs


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

연산자  (0) 2015.05.22
BUFFEREDREADER  (0) 2015.05.21
SCANNER  (0) 2015.05.21
OUTPUT(출력)  (0) 2015.05.21
CASTING(형변환)  (0) 2015.05.21
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
 
import java.util.Scanner;
 
class Scanner 
{
    public static void main(String[] args) 
    {
 
        //Scanner 클래스
        Scanner scan = new Scanner(System.in);
        
        /*
        System.out.print("이름 입력 : ");
        String name = scan.nextLine();
        System.out.printf("안녕~ %s님~\n", name);
        System.out.print("숫자 입력 : ");
        int num = scan.nextInt();
        System.out.printf("%d ^ 2 = %d\n", num, num * num);
        */
 
 
        //스캐너는 문자열을 입력받으면 엔터를 자동으로 삭제 ***
        String str1 = scan.nextLine();
        System.out.println(str1);
 
        //스캐너는 숫자를 입력받은 뒤에는 엔터가 남음. ***
        int num1 = scan.nextInt();
        System.out.println(num1);
 
        scan.nextLine(); //공회전, 뒤에 남은 엔터 소모용.. *
 
        String str2 = scan.nextLine();
        System.out.println(str2);
    }
}
 
cs


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

BUFFEREDREADER  (0) 2015.05.21
INPUT(입력)  (0) 2015.05.21
OUTPUT(출력)  (0) 2015.05.21
CASTING(형변환)  (0) 2015.05.21
ESCAPE  (0) 2015.05.21
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 
class Output 
{
    public static void main(String[] args) 
    {
 
        //에러., Error
        //  - 오류, 예외, 버그 등.,
        
        // 1. 컴파일 에러
        //  - 컴파일 작업 중에 발견되는 에러
        //  - 컴파일러가 발견!
        //  - 문법이 틀려서 발생
        //  - 발생 빈도 높음., 오타.,
        //  - 수정이 쉬움(에러 메시지 출력)
        //  - **** 에러 메시지 발생 -> 복사., 캡쳐.,정리 ****
 
        // 2. 런타임 에러
        //  - 런타임, Runtime(프로그램이 실행중.,)
        //  - 예외(Exception)
        //  - 컴파일 성공! -> 실행중 에러 발생
        //  - 발생 빈도 높음
        //  - 수정 중간.,
        //  - 파일명출력, DB출력, 네트워크입출력., ->예측
 
        //int n = 0; // 사용자에게 입력 숫자
        //에러) Exception in thread "main" java.lang.ArithmeticException: /by zero
        //System.out.println(100 / n);  // => 0입력시 에러 발생
 
        // 3. 논리 에러
        //  - 문법 문제x(컴파일o), 런타임 문제x
        //  - 결과 이상?
        
        //int n = 10;
        //System.out.println(n - 10); //->0 출력
        
 
 
        //콘솔 입출력
        //   - 기본 입력 장치 : 키보드
        //   - 기본 출력 장치 : 모니터
        //   - 기본 에러 장치 : 모니터
 
        //콘솔 출력
        //  1. print 메소드
        //    - 내용을 출력한 뒤 개행(엔터)을 안함
        //  2. println
        //    - 내용을 출력학 뒤 개행을 함
        //  3. pfintf, print format
        //    - 형식 문자를 지원하는 출력 명령어
        //    a. %s : String
        //    b. %d : Decimal, 모든 정수형
        //    c. %f : Float, 모든 실수형
        //    d. %b : Boolean
        //    e. %c : Char
 
 
 
 
        //클래스.필드.메소드();
        //System.out.println();
 
        //String name = "홍길동";
        //System.out.print(name);
 
 
        String name = "홍길동";
        int kor = 100;
        int eng = 90;
        int math = 90;
 
        String name2 = "아무게";
        int kor2 = 80;
        int eng2 = 60;
        int math2 = 80;
 
        //홍길동    100    90    90
        //아무게    80    60    80
 
        System.out.print(name + "\t");
        System.out.print(kor + "\t");
        System.out.print(eng + "\t");
        System.out.println(math);
        
        System.out.println(name2 + "\t" + kor2 + "\t" + eng2 + "\t" + math2);
 
 
 
 
        //printf
        //요구사항] 안녕하세요~ 홍길동님
        //추가사항] 안녕하세요~ 홍길동님 반갑습니다~ 홍길동님
 
        String guest = "홍길동";
 
        System.out.println("안녕하세요~ " + guest + "님");
 
        System.out.println("안녕하세요~ " + guest + "님 반갑습니다~ " + guest + "님");
 
 
        System.out.printf("안녕하세요~ %s님\n", guest);
 
        System.out.printf("안녕하세요~ %s님 반갑습니다~ %s님\n", guest, guest);
 
 
        //SQL
        // insert into tblBoard (seq, title, content, count) values (3, '안녕하세요', '하하하하하', 25);
        String seq = "3";
        String title = "안녕하세요";
        String content = "하하하하하";
        String count = "25";
 
        System.out.println("insert into tblBoard (seq, title, content, count) values (" + seq + ", '" + title + "','" + content + "'," + count + ")");
 
        System.out.printf("insert into tblBoard (seq, title, content, count) values (%s, '%s', '%s', %s);"
                        , seq
                        , title
                        , content
                        , count);   //타입 안맞으면 에러남
  
        int a = 5;
        int b = 10;
 
        System.out.printf("%d + %d = %d\n"
                            , a
                            , b
                            , a + b);
 
        
        char c = '가';
        System.out.printf("문자 : %c\n", c);
 
        boolean flag = true;//false
        System.out.printf("결과 : %b\n", flag);
 
        
        
        //형식 문자 확장
        //    a. %숫자s
        //     - 숫자 : 최소 출력 너비
        //     - 모든 자료형
        //    b. %.숫자f
        //     - 소수 이하 몇자리까지 출력(반올림)
        //     - 실수형
        //  c. %,d
        //   - 천단위 출력(5자리)
        //   - 정수형/실수형
        
        String str = "홍길동";
 
        System.out.printf("[%s]\n", str);
        System.out.printf("[%10s]\n", str);
        System.out.printf("[%-10s]\n", str);
 
        int money1 = 12512;
        int money2 =   150;
        int money3 =  3214;
 
        System.out.println(money1);
        System.out.println(money2);
        System.out.println(money3);
 
        System.out.printf("%10d\n", money1);
        System.out.printf("%10d\n", money2);
        System.out.printf("%10d\n", money3);
 
 
        double d1 = 1234.5678;
        System.out.printf("%f\n", d1);
        System.out.printf("%.2f\n", d1);
        System.out.printf("%.0f\n", d1);
 
 
        int money = 1000000;
 
        System.out.printf("%d원\n", money);
        System.out.printf("%,d원\n", money);
 
 
        double money4 = 1225124.3254;
        System.out.printf("%,15.1f\n", money4);
 
    }
}
 
cs


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

INPUT(입력)  (0) 2015.05.21
SCANNER  (0) 2015.05.21
CASTING(형변환)  (0) 2015.05.21
ESCAPE  (0) 2015.05.21
VARIBALE(변수)  (0) 2015.05.21
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 
class Casting 
{
    public static void main(String[] args) 
    {
 
        //형변환, Type Casting
        // - A라는 자료형을 B라는 자료형으로 볗놘하는 작업
        // - 숫자형 끼리만 가능 ()
        int a = 10;
        int b;
 
        //공간 = 값
        b = a;      //a가 가지고 있는 값을 b에 넣어라(복사해서))
 
        
 
        //1. 안시적인 형변환
        //  - 큰형 = 작은형;        //com:원고지 1칸->2칸에 복사   □  -> □□
        //  - 100% 안전한 작업      
 
        //2. 명시적인 형변환
        //  - 작은형 = 큰형;        //    원고지 2칸->1칸에 복사   □□ ->  □
        //  - 불안전(경우에 따라.. 성공, 실패)
 
        //3. 형변환 연산자
        //  - ()
 
 
 
 
        //예제)
 
        //안시적인 형변환
        byte b1;   //1byte
        short s1;  //2byte
 
        b1 = (byte)-127//원본(풀이:오른쪽에 있는 int를 byte로 바꿔주세요)
 
        //큰형 = 작은형
 
        //short = byte
        //int = short
        //long = byte
 
        //s1 = b1;
        s1 = (short)b1;   //권장, 명시적 (생략가능한 표현 '(shrot)')
        System.out.println(s1);
 
 
 
        //명시적인 형변환
        byte b2;
        short s2;
 
        s2 = 1000//원본
 
        //작은형 = 큰형
        b2 = (byte)s2;  //'(byte)'생략하면 안됨****
        System.out.println(b2); //복사본
 
 
        //예제) 은행잔고와 에러
        int money = 200000000;
        short money2;
 
        money2 = (short)money;
        System.out.println(money2);
 
 
 
 
        //정수 -> 정수
 
 
        //실수 -> 실수
        float f1;
        double d1;
 
        d1 = 3.1245;
 
        f1 = (float)d1;
        System.out.println(f1);  
 
 
 
 
 
        //크로스
        //정수 -> 실수 : 3  >  3.0
        //실수 -> 정수 : 3.5 ->  3  (남는 소수이하 (0.5) 버림)
 
        //실수는 정수보다 크다 ***
        //byte < short < int < long < float < double
        // 1       2      4      8     4       8    byte
        
        int a3 = 3;
        double b3;
        
        // b = (double)a;
        b3 = a3;
        System.out.println(b3);
        
        
        
        int a4;
        double b4;
        b4 = 3.12345;
 
 
        //정수 = 실수
        a4 = (int)b4;
        
        System.out.println(a4);
 
 
 
 
        //숫자형 나머지..
        //  - char형
 
        // A - 65, B-66, C-67   ***외워두기
        // A ~ Z : 65~96 (+25)  대 **
        // a ~ z : 97~122(+25)  소 **
        // 0 ~ 9 : 48~57 (+9)   숫자 **
        // 가 ~ 힝 : 
 
        char c1 = 'A'//A자리에 '힝' '9' 대입해보기
        int code1;
 
        code1 = (short)c1; //풀이: A의 코드값을 int 자리에 넣어라
        System.out.println(code1);
        System.out.println((char)66); //풀이:문자코드값 66에 해당하는 글자를 만들어내라
 
 
 
 
        //사용자에게 영소문자 입력?  -유효성검사
 
        char c2 = 'h'//h의 코드값을 찾아서 범위 안에 있으면 오키
        int code2 = (int)c2;
        System.out.println(code2);  //'h' ->104
        System.out.println(code2 >= 97 && code2 <=122); //true
 
    }
}
 
cs


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

SCANNER  (0) 2015.05.21
OUTPUT(출력)  (0) 2015.05.21
ESCAPE  (0) 2015.05.21
VARIBALE(변수)  (0) 2015.05.21
데이터타입  (0) 2015.05.21
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
 
class Escape 
{
    public static void main(String[] args) 
    {
 
        //Escape Sequence, 제어문자, 특수문자
        //  -  특별한 기능을 하는 문자
        //  -  자료형은 char형
        
 
        // 1. '\n'
        //  - new line
        //  - 개행 문자 (행바꿈)
        String str = " 안 녕 하 세 요. \n \n 반 갑 습 니 다. ";
        System.out.println(str);
 
        // 2. '\r'
        //  - carriage return
        //  - 케럿의 위치를 라인의 처음으로 이동( 거의 사용x)
        System.out.println("하나둘셋\r넷다섯");
 
 
        //엔터
        //  - \r\n
 
        // 3. '\b'
        //  - back space
        //  - 캐럿의 위치를 왼쪽으로 1칸 이동( 거의 사용x, 보통 콘솔 수업시 사용)
        System.out.println("일이\b\b삼사오");
 
        // 4. '/t'
        //  - 탭문자, Tab
        //  - 가장 가까운 탭위치로 이동(위치라인이 정해진, 절대값으로 이동)
        System.out.println("하나\t둘셋넷다섯");
 
        // 5. '\"' '\' '\\'
        //  -  프로그래밍 안에서 특정 역할
 
        //  홍길동 왈 : "안녕하세요~"
        System.out.println("홍길동 왈 : \"안녕하세요~\"");
 
        //에러메시지 illegal escape charactor
        //String path = "D:\Java\Hello.java";
        //System.out.println(path);
 
 
        //숫자형 변수 사용 시 주의사항
 
        //주민번호(-없이 13자리)
        //int jumin1 = 8101132032123;
        //System.out.println(jumin1);
 
        //   25
        //  625
        // 0025
        // 정수 상수 기수법 표현법
        // 1. 10진수 : 10
        // 2.  8진수 : 010
        // 3. 16진수 : 5x10
        //calc계산기 (공학용)
 
        //jumin1 = 0212123011114;
        //System.out.println(jumin1);
 
        //String jumin2 = "0212123011114";
        //System.out.println(jumin2);
 
 
        // - : 숫자 + 숫자 = 숫자
        // - : 문자열 + 문자열 = (결합)문자열 
        // - : 문자열 + 숫자 = ?
        System.out.println("홍길동" + 100 );  //문자열로 출력됨
        System.out.println(100 + "홍길동" );
 
 
        // A + B +C
        // A + B
        // ? + C
        System.out.println(100 + 200 + 300);         //600
         System.out.println(100 + 200 + "300");         //300300
        System.out.println(100 + "200" + 300);         //100200300    
        System.out.println("100" + (200 + 300));     //
 
        int a = 10;
        int b = 20;
        System.out.println("a+b="+(a+b));            //연산 우선순위 주위!
        
    }
}
 
 
 
cs


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

OUTPUT(출력)  (0) 2015.05.21
CASTING(형변환)  (0) 2015.05.21
VARIBALE(변수)  (0) 2015.05.21
데이터타입  (0) 2015.05.21
클래스  (0) 2015.05.21

+ Recent posts