728x90

 

☕ main()메소드 안의 내용은 차례대로 실행된다

- main()메소드 안의 내용이 한줄씩 실행된다. 

- System.out.println("김성박");

   1) 아래의 코드는 "김성박"이라는 이름을 출력한다.

   2) 그리고 줄바꿈을 하게 된다. 그렇기 때문에 다음 줄에 이메일이 출력된다. 

 

public class MyProfile {
    public static void main(String[] args) {
        System.out.println("김성박");
        System.out.println("urstory@gmail.com");
        System.out.println("여자");
    }
}

 

 print()메소드는 줄바꿈을 하지 않는다

- println()메소드는 괄호 안의 내용을 출력하고 줄바꿈 하지만, print()메소드는 괄호 안의 내용만 출력한다 .줄바꿈을 하지 않는다. 

- System.out.println(); 는 아무것도 출력하지 않고 줄바꿈만 한다. 

 

Rectangle 클래스

- 너비가 별 10개, 높이가 별 10개인 사각형을 출력한다

public class Rectangle {
    public static void main(String[] args) {
        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("**********");
        
    }
}

 

높이가 200인 사각형을 출력하려면?

- 만약 높이가 10,000인 사각형을 출력하라고 문제가 바뀐다면?

- 컴퓨터가 가장 잘 하는 일은 반복하는 일이다. 

 

 while 반복문을 이용해 높이가 10인 사각형 출력하기

public class Rectangle2 {
    public static void main(String[] args) {
        int i = 1;
        while(i<=10){
            System.out.println("**********");
            i = i+ 1;
        }
    }
}

- 실행하면, Rectangle 클래스와 같다는 것을 알 수 있다. 

728x90