Day 6(8/8) DoWhile, BreakLoop, Method, Instance

2016. 8. 8. 16:25Programming/Java

[오늘 배운 내용]

1) Do~While 활용

2) Break Loop

3) Heap/Stack 메소드 호출법

4) 객체 생성법


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class L03DoWhile {
     public static void main(String[] args) {
      //do while과 while의 차이
      //do while은 무조건 최초 1회 실행한다.
          int i = 10;
          System.out.println("while 실행");
      while(i<10){
          System.out.println("i: "+i); //출력 안 됨
       i++;
      }
  
          System.out.println("do while 실행");
       i=10;
       do{   
          System.out.println("i: "+i); //i: 10 출력됨
       }while(i<10);
    }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
public class L04Loop {
     public static void main(String[] args) {
  
      //구구단 만들기
     Loop1: for(int i=1; i<10; i++){ //Loop: 반복문에 이름 붙임, if와 같이 쓰면 반복문 다루기 편함
     Loop2: for(int j=1; j<10; j++){
          if(j==5){ break Loop2;} //break Loop: 해당 반복문 빠져나오기
              System.out.print(i+"x"+j+"="+(i*j)+" ");
              }
              System.out.println(); // System.out.print("\n");와 동일
              }
       }
}
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
public class L01Method {
     L01Method(){
         System.out.println("L01Method 호출");
     }
     void heap(){}
     public static void stackMethod(){
         System.out.println("stackMethod 호출");
              //일반 메소드는 thread가 실행될 때 heap 영역에 메모리를 가진다.
              //heap(); 에 오류가 뜨는 이유는 참조할 메모리 위치가 없기 때문.
   //따라서 객체를 생성해 메모리에 저장해야 함
  
L01Method m = new L01Method();
       //new 연산자로 heap 메모리에 L01Method() 객체를 생성하여 메모리에 할당 -> 변수 m이 위치 참조
m.heap(); //객체 m이 가지고 있는 자원 heap()을 실행
}
void heapMethod(){
      System.out.println("heapMethod 호출");
          //L01Method.heap(); //class 명으로 접근하는 것은 static만 가능
  
   this.heap();
  //heapMethod()가 메모리에 할당되는 시점은 new 연산자로 heap 메모리에 생성할 때.
  //new 연산자로 생성된 객체 내부에는 heapMethod()와 heap() 모두 존재.
  //때문에 this(객체 내부 접근) 접근자로 this.heap()을 접근할 수 있다.
  
  L01Method.stackMethod();
  //stackMethod()는 static으로 선언된 메소드로 JVM이 class 파일을 실행 준비할 때 stack 메모리에 할당한다.
  //stackMethod()는 이미 메모리에 할당이 돼있어 참조할 위치가 있다.
  //그래서 class명.method();로 접근 가능(객체 생성 없이도)
 }

  public static void main(String[] args) { //main(jvm 호출) jvm과 함께 stack에 위치
  //heapMethod(), stackMethod()를 호출해 보시오
       //stack 메모리를 참조하는 메소드는 바로 호출 가능
       stackMethod();
       //heap 메모리를 참조하는 메소드는 new 연산자로 객체를 생성 후 호출 가능(아직 메모리 할당 전)
       
  L01Method m = new L01Method(); //생성자 호출
  m.heapMethod();
 m.stackMethod(); //스택도 heap 식으로 참조변수 통해 호출은 가능하지만 권장하지 않음.
   
    } //main
//class
cs


*행 선택 후 alt+방향키로 위치 이동 가능


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class L02Parameter {
 public void paramDemo(int score, String param_name){ //성적 프로그램
 
  char grade = 'A';
  switch(score/10){ // int이기 때문에 자동적으로 9.1 -> 9
  case 9case 10: grade = 'A'break;
  case 8: grade = 'B'break;
  case 7: grade = 'C'break;
  default: grade = 'F'break;   
  }
  
  System.out.println(param_name+"학생의 성적은 "+grade+" 입니다.");
 }
 public static void main(String[] args) { //args -> parameter(매개변수)
      int score = 95;
      String name = "고수";
  
  //학생 성적을 출력하자
  //paramDemo() 매개변수를 준다.
  
  new L02Parameter().paramDemo(score, name);
 } //main
//class

cs


 

<아이디/패스워드 검사하여 로그인 성공하기>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class LoginTest {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("id를 입력해주세요.");
        String id = scan.nextLine();
        System.out.println("pw를 입력해주세요.");
        String pw = scan.nextLine();
        
        //CheckMember의 Login() 호출하기
        CheckMember checkLogin = new CheckMember();
        System.out.println(checkLogin.login(id, pw));
    }
}
 
class CheckMember{
    public String login(String id, String pw){
        if(id.equals("kim")&&pw.equals("1234")){            
            return "로그인 성공";
        }else{
            return "로그인 실패";
        }
    }
}
cs

'Programming > Java' 카테고리의 다른 글

Day 8(8/10) Review (Constructor, Instance)  (0) 2016.08.16
Day 7(8/9) MainMethod  (0) 2016.08.09
Day 5(8/5) For, While  (0) 2016.08.05
Day 4(8/4) IF, Switch  (0) 2016.08.04
Day 3(8/3) Casting & String & Array  (0) 2016.08.04