Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 3. 입출력과 제어문.

Similar presentations


Presentation on theme: "Lesson 3. 입출력과 제어문."— Presentation transcript:

1 Lesson 입출력과 제어문

2 화면에 출력하기 System.out.print 화면에 출력(위치 유지) System.ou.println 화면에 출력 후 줄바꿈
1 : public class Output 2 : { 3 : public static void main(String[] args) 4 : { 5 : System.out.print("Java2 "); 6 : System.out.println("Programming"); 7 : System.out.println("자바2\n프로그래밍"); 8 : } 9 : } System.out.print 화면에 출력(위치 유지) System.ou.println 화면에 출력 후 줄바꿈

3 문자열 연결(+연산자) String sleep = “돌돌이” + “는 어젯밤에 ” “시간”

4 키보드에서 1 문자 읽기 char ch; ch = (char) System.in.read(); // 키보드로부터 1 문자 읽기
형변환(캐스팅)

5 스트림의 이해

6 키보드에서 문자열 읽기 1 : import java.io.*; 2 : 3 : public class Input 4 : {
5 : public static void main(String[] args) throws IOException 6 : { 7 : InputStreamReader isr= new InputStreamReader(System.in); // 리더 연결 8 : BufferedReader in= new BufferedReader(isr); // 버퍼 연결 9 : 10 : String str; 11 : System.out.print("입력: "); 12 : str= in.readLine(); // 키보드로부터 한 줄 입력 13 : System.out.println(str); 14 : } 15 : }

7 if문 1 : public class Compare 2 : {
3 : public static void main(String[] args) 4 : { 5 : int x= 100; 6 : int y= 200; 7 : 8 : if(x>y){ 9 : System.out.println("x가 y보다 큽니다."); 10 : }else{ 11 : System.out.println("y가 x보다 큽니다."); 12 : } 13 : } 14 : }

8 switch문 ... 16 : switch(month){ 17 : case 2:
18 : System.out.println("28일까지 있습니다."); 19 : break; 20 : 21 : case 4: 22 : case 6: 23 : case 9: 24 : case 11: 25 : System.out.println("30일까지 있습니다."); 26 : break; 27 : 28 : default: 29 : System.out.println("31일까지 있습니다."); 30 : break; 31 : 32 : }

9 while문 1 : public class WhileSum 2 : {
3 : public static void main(String[] args) 4 : { 5 : int i= 0; 6 : int sum= 0; // 합이 저장될 변수 7 : 8 : while(i<100) 9 : { 10 : i++; 11 : sum = sum + i; // 값을 누적 12 : } 13 : System.out.println("1부터 100까지의 합: "+ sum); 14 : } 15 : }

10 do-while문 1 : public class DoWhileSum 2 : {
3 : public static void main(String[] args) 4 : { 5 : int i= 0; 6 : int sum= 0; // 합이 저장될 변수 7 : 8 : do{ 9 : i++; 10 : sum = sum + i; // 값을 누적 11 : }while(i<100); 12 : System.out.println("1부터 100까지의 합: "+ sum); 13 : } 14 : }

11 for문 1 : public class ForSum 2 : {
3 : public static void main(String[] args) 4 : { 5 : int sum= 0; 6 : for(int i=0;i<=100;i++){ 7 : sum = sum + i; // 값을 누적 8 : } 9 : System.out.println("1부터 100까지의 합: "+ sum); 10 : } 11 : }

12 break/continue label: while(i<=100) { for(int j=0;j<100;j++)
if(j<i) break label; if(j>i) break; if(j%2==0) continue; } System.out.println("A"); System.out.println("B"); 분기문의 조건으로 이동 이름표 붙은 분기문 밖으로 이동 현재 분기문 밖으로 이동


Download ppt "Lesson 3. 입출력과 제어문."

Similar presentations


Ads by Google