C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息

作者&投稿:何明 (若有异议请与网页底部的电邮联系)
设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息“Rig~

我给他做了一次"大手术",没问题吧?
#include
#include
#include
int main()
{
int x,magic,a=0,c=1;
char b;
do
{
srand(time(NULL)); //为函数rand()设置随机数种子
magic = rand() %100 +1; //magic为1~100间的一个数
printf("%d",magic);
printf("您每次游戏中,只有十次机会
");
while(a<=10)
{
x = NULL;
scanf("%d",&x);
if(x>magic)
printf("Wrong,比magic大
");
else if(x<magic)
printf("Wrong,比magic小
");
else if(x==magic)
{
printf("Right
");
a=a+1;
printf("你猜了%d次了
",a);
break;
}
a++;
printf("你猜了%d次了
",a);
}
a = 10;
if(a==10)
{
b = NULL;
a=0;
getchar();
printf("Do you want to continue?(回答Y/N或者y/n)
");
scanf("%c",&b);
if(b=='y'||b=='Y')
c=1;
else if(b=='n'||b=='N')
c=0;
}
}while(c==1);
return 0;
}

#include #include //time()need#include //rand() and srand()needusing namespace std;int main(){ srand(time(NULL)); //使用rand()前,先设置随机种子,用于保证,不同的时间运行时,可以得到不同的随机数序列 int rd=rand()%1000; //得到一个【0,1000)之间的数。 for( int i=0;i> in ; if ( in==rd ){ cout rd ) cout << "偏大" <<endl; else cout << "偏小" <<endl; } cout << "rand= " <<rd <<endl ; system("pause"); return 0;}

//希望我的回答对你的学习有帮助
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int x, magic, a = 0, c = 1;
char b;
do
{
srand(unsigned(time(NULL))); //为函数rand()设置随机数种子
magic = rand() % 100 + 1; //magic为1~100间的一个数
printf("您每次游戏中,只有十次机会
");
while (a <= 10)
{
scanf("%d", &x);
if (x>magic)
printf("Wrong,比magic大
");
else if (x<magic)
printf("Wrong,比magic小
");
//else if (x = magic) //这里的逻辑运算应该使用“==”
else if (x == magic) //你应该是写错了
{
printf("Right
");
a = a + 1;
printf("你猜了%d次了
", a);
a = 0; //猜对之后,应该对猜测的次数初始化
break;
}
a++;
printf("你猜了%d次了
", a);
if (a == 10)
{
a = 0;
printf("Do you want to continue?(回答Y/N或者y/n)
");
//scanf_s("%c", &b); //这么使用,会将你在输入字符之前的
// 空行(enter键)赋值给 b,而不是你输入的Y/N/y/n
scanf(" %c", &b); //而这种写法,%c 前面的 空格会使 scanf 忽略前面的 enter键
//等待第一个非 enter键 元素读入
if (b == 'y' || b == 'Y')
c = 1;
else if (b == 'n' || b == 'N')
c = 0;
break; //当猜测次数为 10 时,应当跳出
//这里不会自动跳出,因为 a 被初始化为 0
}
}
} while (c == 1);

return 0;
}

scanf 里面没有空格的情况,程序会自动执行 10 次

修改后的程序,猜对的情况

修改后的程序,猜错 10 次,Y的情况

修改后的程序,猜错 10 次,N的情况



将while(a<=10)改为while(a<10)  
将if(a==10){……}的语句放到while(a<10){……}的后面,并去掉if条件

在scanf("%c",&b);前加上getchar();

下面是调整后的语句

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int x,magic,a=0,c=1;
char b;
do

srand(time(NULL)); //为函数rand()设置随机数种子
magic = rand() %100 +1; //magic为1~100间的一个数
printf("您每次游戏中,只有十次机会
");
while(a<10)
{
scanf("%d",&x);
if(x>magic)
printf("Wrong,比magic大
");
else if(x<magic)
printf("Wrong,比magic小
");
else if(x=magic)
{
printf("Right
");
a=a+1;
printf("你猜了%d次了
",a);
break;
}
a++;
printf("你猜了%d次了
",a);
}
a=0;
printf("Do you want to continue?(回答Y/N或者y/n)
");
getchar();
scanf("%c",&b);
if(b=='y'||b=='Y')
c=1;
else if(b=='n'||b=='N')
c=0;
}while(c==1);
return 0;
}


c语言编程:猜数字游戏?
答:include include <stdio.h> include <stdlib.h> define UI unsigned short int void game(){ UI answer;UI input;UI lower=1;UI upper=100;UI count=0;srand(time(NULL));do{answer=rand()%101;} while(answer==0);puts("Welcome to the number guessing game!");do { puts("Please ...

用C语言编写一段猜数字游戏,跟一般的不一样,规则如下
答:进行判断练习,若猜数是50,猜了40,范围变成40到100,若猜60,范围变成40到60.通过循环来控制。include<stdio.h> int main(void){ int a,low,k,he,d;k=50; //k为答案数 low=1;he=100;scanf("%d",&a);while(a!=k){ if(a<k){ low=a;printf("猜数的范围是:%d到%d\n",low...

C语言题目 编程实现数字猜谜游戏!
答:一个函数用于生成一个随机数 再用一个函数进行三次循环 如果循环中输入的数字猜对了就返回1如果三次循环结束则返回0 主数字判断返回的结果如果是1则表示猜对否则表示猜错 然后询问是否继续进行猜数字游戏 下面是一个简单的代码 include <stdio.h>#include void clean_input_buf(void){ while(getc...

C语言编程_幸运52猜数游戏__
答:void sort(double g[],double p[]);void main(){ cout<<"欢迎进入幸运52猜数游戏"<<"t"<<"制作人:电气 C097 张欣瑞 096133"<<endl;double g[N]={12,16,20,40,60,55,91,43,33,50},p[10];char choice;//记录输入的判断是否进行的字符 for(int a=1;;a++){ cout<<"现在开始...

用C语言编写一个“猜数字游戏”的程序
答:scanf("%c",&ch);if(ch == 'a')break;continue;} if(i > 10) //机会用完了 { printf("你的机会已经用完,没机会再猜了\n");break;} } if(i < 3)printf("太棒了\n"); // 对游戏者的评价 else if(i < 5)printf("非常好\n");else if(i < 8)printf("很好\n");...

C语言猜数游戏,有次数限制和是否继续
答:include<stdio.h> include<stdlib.h> int main(){ int s, a;int f = 1;while (f){ system("cls");//DOS清屏命令 printf("操作说明:\n\t输入1继续游戏,0退出游戏,-1直接显示本次结果\n\n");s = rand() % 100;//生成随机数0-100之内 printf("请输入您所猜的数字(0-100) \n...

2. 猜数字(字符串)游戏: 程序功能要求:利用随机函数random()生成1...
答:c语言2.猜数字(字符串)游戏:程序功能要求:利用随机函数random()生成1---N之间的一个整数,输入数字猜该数,每次提示输入的数字与生成的数字之间的大小关系,并统计猜数的次数;猜中... c语言2. 猜数字(字符串)游戏:程序功能要求:利用随机函数random()生成1---N之间的一个整数,输入数字猜该数,每次提示输入的...

c语言猜数字
答:include <stdio.h> include <stdlib.h> include include <conio.h> void main_menu(void);void score_menu(int score);void help_menu(void);int play_game(void) ;int main() { char main_menu_choose;int score = 0;while (1) { main_menu(); //输出主菜单目录 main_menu_choose...

C语言编程猜数字游戏 请大侠看看哪有问题
答:b=b+1;} } guess=guess-(int)(guess/pow(10,i))*pow(10,i);} printf(" %d对%d位置不对\n",a,b);} else if(guess<1000||guess>9999){break;} }} if(a==4){ printf("congratulations!you are so clever\n");printf("do you want to continue???(Y or N)");scanf("%c"...

c语言,编写一个猜数字游戏
答:源码如下:/* File: guess.c */ include <stdio.h> /* standard input & output support */ include <stdlib.h> /* srand() rand() */ include /* time() */ /* 宏定义 */ define NUMBER_LENGTH 5 /* 随机数长度 */ define NUMBER_LIMIT 10 /* 随机数限制, 每...