• 猜数小游戏


     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写一个完整的游戏过程,并在最后有游戏总结。

    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
    ");
    		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);
    
    	}
    		
    }
    

      

  • 相关阅读:
    asp.net大文本保存 framework4.0
    temp文件夹权限对ASP网站以及ASP.NET网站的影响【转载】
    将access数据转移到sql server
    MVC图片上传、剪辑、缩略、水印
    魔兽War3按键精灵Ⅱ(201294)
    PHP中应用CKEditor和CKFinder上传图片读取图片
    C#中get和set个人理解
    asp.net验证码实现
    转三篇文章关于php中session机制
    ORM组件 ELinq (五)映射配置之XML
  • 原文地址:https://www.cnblogs.com/lvcoding/p/7447640.html
Copyright © 2020-2023  润新知