猜数小游戏
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); } } |
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有调度器的协程不是好协程,零基础深入浅出 C++20 协程
· 别做抢活的导演:代码中的抽象层次原则
· 从 Redis 客户端超时到 .NET 线程池挑战
· C23和C++26的#embed嵌入资源指南
· 「EF Core」框架是如何识别实体类的属性和主键的
· 阿里巴巴为什么禁止超过3张表join?
· 博客园众包线下沙龙第1期:云栖开发者基地,共建技术新天地
· 让 AI 帮我部署网站,太方便了!
· 别做抢活的导演:代码中的抽象层次原则
· .NET周刊【7月第1期 2025-07-06】