猜数字游戏

(1)每局游戏,提示“Game start!”,并随机产生一个 1~50的数让用户猜,用户基础分为10分,猜错一次扣 1 分。
(2)如果用户猜错,则提示“Too big!”还是“Too small!”。
(3)如果猜了10次均未猜中,则提示“You Failed! The number is x”,x为目标数字。
(4)如果猜对,则提示“Congratulation!Your guessing
Level is S/A/B/C/D/E”,其中level由用户剩余分数决定。
S: 10分 A:9分 B:8分 C:7分 D:6分 E:1~5分
(5)每局结束后,询问用户“Play again?”,如果用户输入“y/Y”则再来一局,否则结束游戏。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void guess(){
	int i=0;//标志位,是否作答正确
	int score=10;
	int guesscount;
	srand(time(NULL));//设置一个随机种子,每次运行都能保证随机种子不同
	guesscount = rand()%50+1;//生成1-50随机数
	printf("%d\n",guesscount);//输出随机数,便于检测程序
	while(score!=0&&i==0){//循环,结束条件:分数为0或已猜数数字
		int num;
		scanf("%d",&num);
		if(num<guesscount){
			printf("Too small!\n");
			score--;
		}
		else if(num>guesscount){
			printf("Too big!\n");
			score--;
		}
		else{
			char ch;
			switch(score){//switch分支确定等级
				case 10:ch='S';break;
				case 9:ch='A';break;
				case 8:ch='B';break;
				case 7:ch='C';break;
				case 6:ch='D';break;
				case 5:ch='E';break;
				case 4:ch='E';break;
				case 3:ch='E';break;
				case 2:ch='E';break;
				case 1:ch='E';break;
			}
			printf("Congratulation!Your guessing Level is %c\n",ch);
			i=1;
		}
	}
	if(score==0){
		printf("You You Failed! The number is %d\n",guesscount);
	}
	printf("Play again?(Y/y继续)\n");
}
int main(){
	char ch1;
	do{//do-while循环第一次不判定
		printf("Game start!\n");
		guess();
		char ch2;
		getchar();//吸收回车
		scanf("%c",&ch2);
		ch1=ch2;
	}while(ch1=='y'||ch1=='Y');
	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值