* 업다운 게임

숫자 1~100까지의 하나의 랜덤숫자를 맞춰라

5회안에 맞혀야 성공

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
import java.util.Scanner;
 
public class Ex05 {
 
    double ran = Math.random();        //random함수
 
    int com = (int)(100*ran+1);        //1-100의 숫자 com 에 저장, +n 의 n은 시작숫자
 
    public static void main(String[] args)  throws Exception{
        Ex05 me = new Ex05();
 
        System.out.println("---------GAME START---------");
 
        me.game();        //game()호출
 
        System.out.println();
        System.out.println("----------GAME END----------");
 
    }//main end
 
    void game(){
 
        Scanner sc = new Scanner(System.in);
        int number;
 
        //System.out.println(com);        //랜덤숫자 정답
 
        for(int i=0;i<5;i++){
 
            System.out.print("1-100까지의 숫자 입력 >>> ");
 
            number=sc.nextInt();        //입력
 
            if(number<1 || number>100){            //1-100까지의 숫자가 아니면 다시 입력, count숫자도 -1 감소
                System.out.println("1-100까지의 숫자를 다시 입력하십시오");
                i--;
            }else if(number<com){
                System.out.println(number+"보다 큽니다");
            }else if(number>com){
                System.out.println(number+"보다 작습니다");
            }else{
                System.out.println("성공!!! "+(i+1)+"회 만에 성공하셨습니다!");    //i=0부터 시작이라 +1
                break;
            }
 
            System.out.println();
 
            if(i==4){
                System.out.println("실패..."+com+"이였습니다");
            }
        }//for end
    }//game end
}//class end
cs

결과


'* Programming > JAVA' 카테고리의 다른 글

DAY12 배열  (0) 2016.07.19
DAY11 class 정리/상수화  (0) 2016.07.18
DAY10 Class/생성자  (0) 2016.07.18
DAY9 멤버필드  (0) 2016.07.15
Apache Ant  (0) 2016.07.13

+ Recent posts