c语言,编写一个猜数字游戏

作者&投稿:夔标 (若有异议请与网页底部的电邮联系)
c语言编程 编一个猜数字游戏~

源码如下:
/* File: guess.c */
#include /* standard input & output support */
#include /* srand() rand() */
#include /* time() */
/* 宏定义 */
#define NUMBER_LENGTH 5 /* 随机数长度 */
#define NUMBER_LIMIT 10 /* 随机数限制, 每一位0-9 */
#define INPUT_LENTH 128 /* 输入缓冲区大小 */
char goal[NUMBER_LENGTH] = {0}; /* 保存随机数 */
char flag[NUMBER_LIMIT] = {0}; /* 保存随机数标志, 保证不重复 */
char input[INPUT_LENTH] = {0}; /* 保存输入 */
/* 初始化用于保存数据的数组 */
void initData()
{
int i = 0;
while (i < NUMBER_LENGTH)
goal[i++] = 0;

i = 0;
while (i < NUMBER_LIMIT)
{
flag[i++] = 0;
}
}
/* 初始化用于保存缓冲区的数组 */
void initBuffer()
{
int i = 0;
while (i < INPUT_LENTH)
input[i++] = 0;
}
/* 显示猜测结果 */
void display()
{
int count = 0;

int i = 0;
while (i < NUMBER_LENGTH)
{
if (input[i] == goal[i])
{
printf("%c", 'o');
count++;
}
else
{
printf("%c", 'x');
}

i++;
}

printf("
RIGHT: %d bit(s)
", count);

if (count == NUMBER_LENGTH)
{
printf("You win! The number is %s.
", goal);

exit(0);
}
}
/* 生成随机数 */
void general()
{
/* 以时间作为时间种子保证生成的随机数真正具有随机性质 */
srand((unsigned int)time(NULL));

int i = 0;
while (i < NUMBER_LENGTH)
{
char tmp;
do
{
tmp = '0' + ((i != 0) ? (rand() % 10) : (1 + rand() % 9));
} while (flag[tmp] != 0);

flag[tmp] = 1;
goal[i++] = tmp;
}
}
/* 输入方法,用于猜测 */
void guess()
{
printf("Please input the number you guessed:
");
scanf("%s", input);
display();
initBuffer();
}
/* 主函数,程序主框架 */
int main (int argc, const char * argv[])
{
initData();
initBuffer();
general();
while (1) guess();
return 0;
}
==============================================
运行结果见附图,希望我的回答能够对你有所帮助。

同学你好!
下面是我自己做了一个,运行是成功的。希望对你有所帮助!

#include
#include
#include
#include

void main()
{
int i,j,cnt,guard=1;
while(guard)/*利用guard来判断是否继续进行游戏*/
{
srand((unsigned)time(NULL));
i=rand()%100;/*随机生成整数*/
cnt=0;/*比较次数置零*/
for(;;)
{
printf("Please input the data:");
scanf("%d",&j);
cnt++;/*次数累加*/
if(j>i)
printf("Too high!
");
else if(j<i)
printf("Too low!
");
else
{
printf("Right!
");
printf("The cnt is:%d
",cnt);
printf("Wanna continue? 1.continue 0.exit
");
scanf("%d",&guard);/*选择是否继续*/
break;
}
}
}
}

希望我的回答对你有帮助,祝你的C成绩进步

源码如下:

/* File: guess.c */

#include <stdio.h>  /* standard input & output support */

#include <stdlib.h> /* srand() rand() */

#include <time.h>   /* time() */

/* 宏定义 */

#define NUMBER_LENGTH   5   /* 随机数长度 */

#define NUMBER_LIMIT    10  /* 随机数限制, 每一位0-9 */

#define INPUT_LENTH     128 /* 输入缓冲区大小 */

char goal[NUMBER_LENGTH]    = {0};  /* 保存随机数 */

char flag[NUMBER_LIMIT]     = {0};  /* 保存随机数标志, 保证不重复 */

char input[INPUT_LENTH]     = {0};  /* 保存输入 */

/* 初始化用于保存数据的数组 */

void initData()

{

    int i = 0;

    while (i < NUMBER_LENGTH)

        goal[i++] = 0;

    

    i = 0;

    while (i < NUMBER_LIMIT)

    {

        flag[i++] = 0;

    }

}

/* 初始化用于保存缓冲区的数组 */

void initBuffer()

{

    int i = 0;

    while (i < INPUT_LENTH)

        input[i++] = 0;

}

/* 显示猜测结果 */

void display()

{

    int count = 0;

    

    int i = 0;

    while (i < NUMBER_LENGTH)

    {

        if (input[i] == goal[i])

        {

            printf("%c", 'o');

            count++;

        }

        else

        {

            printf("%c", 'x');

        }

        

        i++;

    }

    

    printf("
RIGHT: %d bit(s)
", count);

    

    if (count == NUMBER_LENGTH)

    {

        printf("You win! The number is %s.
", goal);

        

        exit(0);

    }

}

/* 生成随机数 */

void general()

{

    /* 以时间作为时间种子保证生成的随机数真正具有随机性质 */

    srand((unsigned int)time(NULL));

    

    int i = 0;

    while (i < NUMBER_LENGTH)

    {

        char tmp;

        do

        {

            tmp = '0' + ((i != 0) ? (rand() % 10) : (1 + rand() % 9));

        } while (flag[tmp] != 0);

        

        flag[tmp] = 1;

        goal[i++] = tmp;

    }

}

/* 输入方法,用于猜测 */

void guess()

{

    printf("Please input the number you guessed:
");

    scanf("%s", input);

    display();

    initBuffer();

}

/* 主函数,程序主框架 */

int main (int argc, const char * argv[])

{

    initData();

    initBuffer();

    general();

    while (1) guess();

    return 0;

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

运行结果见附图,希望我的回答能够对你有所帮助。



#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand((unsigned)time(0));
int count=5;
int n=rand()%99+1,in;
while(count>0)
{
printf("请输入你猜的数:");
fflush(stdin);
scanf("%d",&in);
if(in==n)
{
printf("
恭喜你猜对了!
");
getchar();
exit(0);
}
else if(in>n)
{
printf("
太大");
getchar(); 
}
else if(in<n)
{
printf("
太小");
getchar();
}
count--;
printf(" ---你还有%d次机会

",count);
}
printf("
5次机会用完
");
getchar();
return 0;
}


太简单了,给你个编程思路吧:
输入甲猜的数
循环5次,每次都输入一个乙的数,并判断与甲数的关系
如果大则输出你猜的数据大了
如果小则输出你猜的数据小了
如果相同则输出你赢了!游戏结束。并结束程序
如果循环结束都没有猜对,就输出你输了,游戏结束,并输出甲输入的数据
提示:输入用scanf函数
循环可以用for循环
数据可以用整形

分析:
先产生一个随机数N。
然后输入数I,如果A大于N,则提示大于信息。
如果I小于N,则提示小于信息。
直到I==N,则输出成功信息。
这是我用C语言写的。
环境:
WIN-C ,TORBO C,如果是C++环境把倒数第二排getch();删掉!
已经调试成功:
main()
{
int i=0,n;
srand(time(0));
n=rand()%100+1;
while(i!=n)
{printf("please input a number:\n");
scanf("%d",&i);
if(i>n)printf("this number is too big!\n");
if(i<n)printf("this number is too smaller!\n");
}
if(i==n)
printf("PASS!%3d",n);
getch();
}
提示:
srand(time(0));
n=rand()%100+1;
是用来生成一个1~100以内的随机数,如果你改,把100改成50或者200。如(n=rand()%50+1;

求采纳为满意回答。

超简单啊。。。。建立3个int对象 要猜的数 猜的数 猜的次数
判断 猜的次数<5 执行 次数++然后让输入 =猜的数 判断猜的数=要猜的 是(输出猜对)否继续执行 判断 猜的数>要猜的(是输出数据大了)否 输出数据小了。。。


用C语言编写一个具有简单界面的猜数字游戏
分析:先产生一个随机数N。然后输入数I,如果i大于N,则提示大于信息。如果I小于N,则提示小于信息。直到I==N,则输出成功信息。这是我用C语言写的。环境:WIN-C ,TORBO C,如果是C++环境把倒数第二排getch();删掉!已经调试成功:main(){ int i=0,n;srand(time(0));n=rand()%100+1;w...

C语言程序设计:用电脑设计一个猜数游戏,电脑从1-100中随机选一个数让...
曾经帮别人答过,代码如下:include <stdio.h>#include <stdlib.h>#include int main(){ int nAppValue, nUserValue, iCount = 0; srand( time( 0 ) ); nAppValue = rand() % 100 + 1; printf( "请猜我选中的是哪个数字(1-100)?\\n" ); do { scanf( "%d...

c语言怎么使用随机函数rand编写一个猜数字的游戏程序?
,&b)){ for(i=0;i<N;++i)rand()%10;a = rand()%100;while(1){ if(a == b){puts(" 恭喜你 猜对了 !\\n***\\n");break;} elseif(a > b)puts("你输入的数 小了!");else puts("你输入的数 大了!");scanf("%d",&b);} } system("pause");} ...

用VB语言编写一个猜数字的游戏
例如:计算机的随机数字为:1234 ,我猜的数字为:1356 ,那么这时计算机会给你提示为:1A1B,也就是说,你猜的数字中,有一位数字是猜对的,而且数字位置都对,所以显示为1A;还有一个数字也猜对了,但是位置不对,所以显示为1B。就这些了,看谁猜的次数少。首先在Form中加入一个CommandButtion...

用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.不要用goto语句\/\/2.为什么要用float ?#include <stdio.h>#include <stdlib.h>#include int main(){srand((unsigned)time(NULL));int s,n;s=rand()%101;while(1){system("cls");printf(" 1. play\\n");printf(" 0. exit\\n");printf("\\n请输入0或1:");fflush(stdin)...

c语言猜数字游戏
include <stdio.h>#include <stdlib.h>#include <string.h>#include char* getHint(char* secret, char* guess){int i ,countA=0, countB=0 ;int count[10] = {0};char *result = (char *)malloc(strlen(secret)); if(strlen(secret)!=strlen(guess)) {printf("Unequal Length...

用C语言编写一个“猜数字游戏”的程序
include<stdio.h> include<stdlib.h> void main(){ int Win = rand() % 90 + 10; \/\/随机赋值 int i = 0;int n;char ch;printf("please input n\\n");scanf("%d",&n);while(1){ if(n > Win) \/\/猜的数字大了 { printf("you guess number big\\n");i++;scanf("%d"...

c语言猜数字游戏系统随机产生一个四位数 玩家进行猜测 系统提示高了...
include <stdio.h> include include <stdlib.h> void main(){ srand((unsigned)time(NULL));bool flig=true;int a[4];for (int i=0,j=0;i!=15;++i){ if (flig){ printf("开始猜第%d个数:\\n",j+1);a[j]=rand()%100+1; \/\/产生 1~100的随机数 \/\/ printf("%d",a[...

用C语言编写猜数字(喜欢挑战的人可以来看看哦)
\/\/ 猜数字.cpp : Defines the entry point for the console application.\/\/ include <stdafx.h> include <stdlib.h> include <stdio.h> include \/\/随机生成4位数,要求没有重复数字 void ProduceRandomNumber(int data[4]){ int z;\/*随机选取1-9999的数,放弃1-999的数,选择1000-9999的数*...

宿豫区17213444953: c语言,编写一个猜数字游戏 -
希钱千咳: 太简单了,给你个编程思路吧: 输入甲猜的数 循环5次,每次都输入一个乙的数,并判断与甲数的关系 如果大则输出你猜的数据大了 如果小则输出你猜的数据小了 如果相同则输出你赢了!游戏结束.并结束程序 如果循环结束都没有猜对,就输出你输了,游戏结束,并输出甲输入的数据 提示:输入用scanf函数 循环可以用for循环 数据可以用整形

宿豫区17213444953: c语言:猜数字游戏代码 -
希钱千咳: #include<stdio.h> #include<stdlib.h> #include<time.h>int main() {int num,n,i,cnt=0,finish=0;srand((unsigned int)time(NULL));num=rand()%100;printf("请猜数字,0~100之间\n");do{scanf("%d",&i);cnt++;if(i<0&&i>=100){printf("...

宿豫区17213444953: 用C语言编写一个具有简单界面的猜数字游戏 -
希钱千咳: 分析: 先产生一个随机数N. 然后输入数I,如果i大于N,则提示大于信息. 如果I小于N,则提示小于信息. 直到I==N,则输出成功信息. 这是我用C语言写的. 环境: WIN-C ,TORBO C,如果是C++环境把倒数第二排getch();删掉! 已经...

宿豫区17213444953: 用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;...

宿豫区17213444953: 用c语言编写一个猜数游戏 -
希钱千咳: #include <stdio.h> #include <stdlib.h> #include <time.h> #define Range 100 int guess(int k){ int input; printf("请输入数字:\t"); scanf("%d",&input); if(input==k)return 1; if(input>k)return -1; if(input<k)return -2; } void score(int time){ if(time<5){...

宿豫区17213444953: c语言编写猜数字游戏 -
希钱千咳: #include<stdio.h> #include<time.h> #include<math.h> int main() { int i,a,n; srand((int)time(NULL)); n=rand()%100+1; for(i=0; i<5; i++) { printf("请输入一个介于1到100的整数:"); scanf("%d",&a); if(a<n) printf("猜小了!\n"); if(a>n) printf("猜大了!\n"); if(a==n) { printf("猜对了!\n"); break; } } return 0; }

宿豫区17213444953: 猜数游戏C语言程序设计 -
希钱千咳: 这道题不难,只要知道怎样用c语言生成1~100的随机数就很好办了!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附代码如下! #include<stdio.h> #include <...

宿豫区17213444953: 用c语言编一个猜数小游戏,请大家帮帮忙 -
希钱千咳: 展开全部#include #include #include void create(int *secretp) { srand(time(NULL)); *secretp = rand() % 100 + 1; } void guess(int secret) { int n, i = 0; do { printf("请输入您猜的数字:"); scanf("%d", &n); if(n < secret) printf("小了!\n"); if(n > ...

宿豫区17213444953: C语言 猜数字程序 -
希钱千咳: 原因分析: “scanf("%d",&a);”这一行,输入完一个数字后,你还必须按下回车键,所以这行执行完毕后缓冲区就还留着一个'\n'字符即回车字符.执行到“}while(ch=getchar()!='n');”时,这字符就直接赋给了ch(验证方法:把“while(ch...

宿豫区17213444953: c语言 ,编写一个猜数游戏程序,输入一个设定的整数,供玩者猜. -
希钱千咳: #include #include#define GUESS_LIMIT 1000 char *infos[] = {"【猜数游戏已启动】\n数字已经设定好,请玩家进行猜测,数字范围在0~%d\n","猜测数字大了!请继续猜测:","猜测数字小了!请继续猜测:","恭喜猜中数字!...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网