猜数小游戏

复制代码
 1 #include<iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 int count = 0;
 5 bool judge (int num,int ynum){
 6     if(num == ynum){
 7         cout<<"You got it right in "<<count<<" guesses"<<endl;
 8         return true;
 9     }
10 
11     else if (num>ynum){
12         cout<<"It`s higher."<<endl;
13         return false;    
14     }
15     else{
16         cout<<"It`s lower."<<endl;
17         return false;    
18     }
19 }
20 int main(){
21     int num = rand()%100+1;
22     int ynum;
23     cout<<"I`m thinking of a number between 1 and 100..."<<endl;
24     bool nether= false; 
25     while(!nether){
26         count++;
27         cout<<"Your guess? ";
28         cin>>ynum;
29 //        cout<<endl;
30         nether = judge(num,ynum);
31         
32     }
33 }
复制代码

 上面的是用C++写的简单的一个回合的游戏,现在用Java写一个完整的游戏过程,并在最后有游戏总结。

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
package LAB1;
import java.util.*;
 
public class Guess {
    public static final int range=100;
    static int best=100000,total_num=1,guess_times_sum=0;
 
    public static void main(String[] args){
        Scanner console=new Scanner(System.in);
        Random rand=new Random();  
        System.out.println("Welcome to the Guessing Game program\n");
        best=Game(console,rand);
        Again(console,rand);
        System.out.println("  best game   = "+best);
    }
     
    public static int Game(Scanner console,Random rand){
        System.out.println("I'm thinking of a number between 1 and "+range+"...");
        int guesstimes=0;
        int guessnum;
        int randnum=rand.nextInt(range);
        do {
            System.out.print("Your guess? ");
            guessnum=console.nextInt();
            if(guessnum>randnum){
                System.out.println("It's lower.");
            }
            else if(guessnum<randnum){
                System.out.println("It's higher.");
            }
            guesstimes++;
        }while(guessnum!=randnum);
        System.out.println("You got it right in "+guesstimes+" guesses");
        guess_times_sum+=guesstimes;
        if(best>guesstimes){
            best=guesstimes;
        }
        return best;
    }
     
 
    public static void Again(Scanner console,Random rand){
        System.out.print("Do you want to play again? ");
        String YorN=console.next();
        if(YorN.startsWith("y")||YorN.startsWith("Y")){
            System.out.println();
            Game(console,rand);
            total_num++;
            Again(console,rand);
 
        }
        else if(YorN.startsWith("n")||YorN.startsWith("N")){
            end(console,rand,total_num,guess_times_sum);
        }
        else{
            Again(console,rand);
        }
    }
     
    public static void end(Scanner console,Random rand,int m,int n){
        System.out.println();
        System.out.println("Overall results:");
        System.out.println("  total games   = "+m);
        System.out.println("  total guesses = "+n);
        System.out.println("  guesses/game  = "+(double)n/m);
 
    }
         
}

  

posted @   Mr.Struggle  阅读(273)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示