干货 | C语言实现一款猜数字游戏【附源码】!

本文主要向大家介绍了C/C++知识点之用C语言实现一款猜数字游戏!

通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

最经典的猜数字游戏的例子来展示条件结构在程序中的作用,今天看到另外一种猜数字的玩法,也挺有趣:

这个游戏由三个人一起玩,一个人做主持人,心中默想一个1到100之间的数字,然后由其他两个人轮流猜,每次猜测之后,主持人就说出猜测的这个数比他心中的数字是大还是小,然后另外一个人根据这个信息继续猜测,如此轮流,最后谁猜中就算谁输了。(算赢了也可以)

这是一个相互挖坑让对方跳的过程,特别是最后几步,猜测范围越来越小,真是步步惊心,稍不留意,就踩到对方挖的坑里去了。

===========================

代码如下

*/

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<stdbool.h>

#include<ctype.h>

intmain()

{

srand(time(NULL));  //随机数种子

while(true)

{

intmin=1;

intmax=100;  //初始范围

intcount=0;  //猜测次数

constinttarget=rand()%max+1;  //产生随机数的目标数

while(true)

{

intguess=0;

printf("pleaseinputanumberbetween%dand%d\n",min,max);

fflush(stdin);  //清空输入缓存,以便不影响后面输入的数。比如你逐个输入字符,他帮你缓冲掉你每输入一个字符后面所敲的回车键。否则回车也会被当成字符保存进去。scanf("%d",&guess);//获取猜测的数字

++count;

if(guess<min||guess>max)  //超出范围

{

printf("theinputisoutof%d-%d\n",min,max);

continue;

}

else

{

if(target==guess)//猜中

{

printf("YOUWIN!\nyouhaveguessed%dtimesintotal.\n",count);

break;

}

elseif(target>guess)  //目标比猜的数字大

{

min=guess;

printf("thetargetislargerthan%d\n",guess);

}

else  //目标比猜的数字小

{

max=guess;

printf("thetargetislessthan%d\n",guess);

}

}

}

//本轮游戏结束,是否继续

printf("Doyouwanttoplayagain?(Y-yes,N-no)\n");

fflush(stdin);

charc='Y';

scanf("%c",&c);

if(toupper(c)!='Y')

{

break;

}

}

return0;

}

对于热爱编程的小伙伴来说,一个好的学习环境和学习氛围是事半功倍的!如果你想要更多学习资源有需求的话,笔者这里有一个编程零基础入门学习交流俱乐部,想进来学习的可以私我【编程学习】!还有完整的学习路线图和学习文件视频,欢迎想学编程的小伙伴们!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值