3.猜数游戏 (1)游戏开始,提示"Ganestart!",并随机产生一个1^90 的数让用户猜,用户基础分为10分,猜错一次扣1分。 (2)如果用户猪错,则提示“Too big! ”还是“Too small! (3)如果猜了10次均未猜中,提示"You Failed! The aumber is r”,r为目标数字。 (4)如果猜对,则提示"Congratulation! Your gessind Lerel is S/A/B/C/D/E" ,其中level由用户剩余分数决定。 S: 10分A:9分B:8分C:7分D:6分E: 1^5分
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int ret = 0;
int i = 0;
int rnd = 0;
int level = 10;
srand((unsigned int)time(NULL));
ret = rand()%90;
printf("Ganestart!\n");
while(1)
{
printf("请输入一个数:");
scanf("%d",&rnd);
if(rnd>ret){
printf("Too big! \n");
}else if(rnd<ret){
printf("Too small! \n");
}else{
break;
}
level--;
if(level<=0){
printf("You Failed! The aumber is r");
exit(0);
}
}
switch(level){
case 10:
printf("Congratulation! Your gessind Lerel is S");
break;
case 9:
printf("Congratulation! Your gessind Lerel is A");
break;
case 8:
printf("Congratulation! Your gessind Lerel is B");
break;
case 7:
printf("Congratulation! Your gessind Lerel is C");
break;
case 6:
printf("Congratulation! Your gessind Lerel is D");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Congratulation! Your gessind Lerel is E");
break;
}
return 0;
}
用二分法的话,根本不需要10次肯定就能猜中了啊
作业要自己做。实在不会再上来求助。这代码百度都烂大街了。
你是哪里不会,先写自己会部分,有不会的再来问
#include <stdlib.h>
#include<time.h>
int main(){
printf("game start!\n");
srand((unsigned)time(NULL));//选取种子文件
int score = 10;
int n = rand()%90;
int m = 0;
while(1)
{
printf("请输入你猜测的数字:");
scanf_s("%d",&m);
if(m == n)
{
char ch;
switch(score)
{
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;
default:
ch = 'E';
break;
}
printf("Congratulation! Your gessind Lerel is %c",ch);
break;
}
else if(m>n)
{
printf("Too big!\n");
score--;
}
else
{
printf("Too small!\n");
score--;
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int ret = 0;
int i = 0;
int rnd = 0;
int level = 10;
srand((unsigned int)time(NULL));
ret = rand()%90;
printf("Ganestart!\n");
while(1)
{
printf("请输入一个数:");
scanf("%d",&rnd);
if(rnd>ret){
printf("Too big! \n");
}else if(rnd<ret){
printf("Too small! \n");
}else{
break;
}
level--;
}
switch(level){
case 10:
printf("Congratulation! Your gessind Lerel is S");
break;
case 9:
printf("Congratulation! Your gessind Lerel is A");
break;
case 8:
printf("Congratulation! Your gessind Lerel is B");
break;
case 7:
printf("Congratulation! Your gessind Lerel is C");
break;
case 6:
printf("Congratulation! Your gessind Lerel is D");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Congratulation! Your gessind Lerel is E");
break;
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632