티스토리 뷰

1. 스택틱 변수의 다른 용어 3가지는?
 공유변수, 클래스 변수, 정적 변수

 



2.스태닉 변수의 접근 방법은?
 static함수를 만든다

 



3.스태틱 변수의 활용의 예를 드시오.


4.스태틱 함수에 인스턴스 라면이 올 수 없는 이유는?
static 객체가 생성 되기 전에, 인스턴스 변수 및 함수는 메모리에 객체가 생성 될 때 올라간다.
즉 인스턴스 라면은 static 객체가 생성 되기 전에 메모리에 올라가는 것이다.
static과 인스턴스 메모리 생성에 시간 차가 있는 것이다.

 



5. 아래의 프로그램에서 기존에 값을 다이렉트로 넣었던 부분을 Scanner 로 입력 받아 처리 하시오.

 

int math, science, english;
math = 90;
science = 80; 
english = 80;

Grade me = new Grade(math, science, english);
System.out.println("평균은 " + me.average());
System.out.println(me.getGrade()); //우 입니다.

 

 

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
import java.util.Scanner;
 
class Grade {
    private int math, science, english;
 
    public Grade(int math, int science, int english) {
        this.english = english;
        this.math = math;
        this.science = science;
    }
 
    public double average() {
        return ((double)(math + science + english) / 3);
    }
 
    public String getGrade() {
        String str;
 
        if (average() >= 90) {
            str = "수 입니다.";
        } else if (average() >= 80) {
            str = "우 입니다.";
        } else if (average() >= 70) {
            str = "미 입니다.";
        } else if (average() >= 60) {
            str = "양 입니다.";
        } else {
            str = "가 입니다.";
        }
        return str;
    }
 
}
 
public class GradeScanner {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        System.out.print("수학성적: ");
        int math = sc.nextInt();
        System.out.print("과학성적: ");
        int science = sc.nextInt();
        System.out.print("영어성적: ");
        int english = sc.nextInt();
 
        Grade me = new Grade(math, science, english);
        System.out.println("평균은 " + me.average());
        System.out.println(me.getGrade());
 
    }
 
}
 
 
 
 
cs

 

 

 


6.아래의 가위바위보 게임을 짜시오.

-난수 발생 함수가 필요 할것입니다. 아래의 메소드를 참고 해 주세요.
- (Math.random() * 3 + 1);

출력=======================
가위, 바위, 보 중 하나를 입력하세요.
가위
바위
졌습니다.
계속하시겠습니까?(Y/N)
y
가위, 바위, 보 중 하나를 입력하세요.
보
보
비겼습니다.
계속하시겠습니까?(Y/N)


힌트1: 
1. [ 계속하시겠습니까? ] 말고 [ 한번만 실행 ]하는걸 먼저 짜본다.
2. 코딩 자체를 while(true) 문으로 돌려보셈. 그럼 계속 돌겠지?
3. 그 때 System.out.println("계속하시겠습니까?(Y/N)")
   char yesOrNo = scanner.next().charAt(0); 
한 담에 break문으로 빠져나와 

힌트2:
변수 2개 ~ int로든 문자로든 알아서 표현 
1)나의 가위바위보 
2)컴퓨터의 가위바위보
3) if든 switch문이든 9개 표현.
(난수 123을 표현해서 함수 만들면 케이스가 3*3 =9개)
4)그러면서 while문 끝까지 돌려

 

 

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
import java.util.Scanner;
 
 
class Player{
    private int rsp; // 1.가위    2.바위   3.보
    
    public Player(String rsp) {
        if(rsp.equals("가위")) {
            this.rsp = 1;
        }
        else if(rsp.equals("바위")) {
            this.rsp = 2;
        }
        else {
            this.rsp = 3;
        }
    }
    
    
    public Player() {
        this.rsp = (int)(Math.random()*3 + 1);
                
    }
    
    
    private String getRSPString(int rsp) {
         String str;
        
         if(rsp == 1)
         str = "가위";
         else if(rsp ==2)
         str = "바위";
         else
         str = "보";
        
         return str;
        }
        
    
    public void result(Player player) {
        
        System.out.println("나는:" +  getRSPString(this.rsp) + " 당신은:" + getRSPString(player.rsp));
        
        if(this.rsp == player.rsp) {
            System.out.println("비겼습니다.");
            return;
        }
        if(this.rsp == 1 && player.rsp == 2) {
            System.out.println("제가 졌습니다.");
        }
        else if(this.rsp == 1 && player.rsp == 3) {
            System.out.println("제가 이겼습니다.");
        }
        else if(this.rsp == 2 && player.rsp == 1) {
            System.out.println("제가 이겼습니다.");
        }
        else if(this.rsp == 2 && player.rsp == 3) {
            System.out.println("제가 졌습니다.");
        }
        else if(this.rsp == 3 && player.rsp == 1) {
            System.out.println("제가 졌습니다.");
        }
        else if(this.rsp == 3 && player.rsp == 2) {
            System.out.println("제가 이겼습니다.");
        }
        
    }
    
    
}
 
 
public class GameRSP {
 
    public static void main(String[] args) {
        
        while(true) {
            Scanner sc = new Scanner(System.in);
            
            System.out.println("가위 바위 보를 입력하세요.");
            String rsp = sc.next();
            
            Player you = new Player(rsp);
            Player com = new Player();
            
            com.result(you);
            
            System.out.println("계속 : Y   /  중단 : N");
            char ch = sc.next().charAt(0);
            
            if(ch == 'N' || ch == 'n') {
                break;
            }        
        }
        
        System.out.println("게임 종료 입니다.");
        
    }
 
}
cs

 

 

 

 

댓글