基于猜数字游戏程序逐步完善(C语言基础练习)
条件控制的循环利用
1 利用随机数的生成,完成猜数字游戏程序
#include<stdio.h>
#include<stdlib.h>
main()
{
int magic,guess;
magic=rand();
printf(“Please input a magic number:”);
scanf("%d",&guess);
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
return 0;
}
//猜对数字为止 同时为猜数过程实现计数
#include<stdio.h>
#include<stdlib.h>
main()
{
int magic,guess;
int counter=0;//设置计数
magic=rand()%100+1;//生成1到100的随机数
counter++;
do{
printf(“Please input a magic number:”);
scanf("%d",&guess);
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
}while(guess!=magic);
printf(“counter=%d\n”,counter);
return 0;
}
// 调用标准库函数srand()为函数设置随机数种子 改变随机数序列
#include<stdio.h>
#include<stdlib.h>
main()
{
int magic,guess;
int counter=0;//设置计数
unsigned int seed;
printf(“please input a number:”) ;//随机数种子输入
scanf("%d",&seed);
srand(seed);//设置不同的随机数种子,以产生不同的随机数序列
magic=rand()%100+1;//生成1到100的随机数
counter++;
do{
printf(“Please input a magic number:”);
scanf("%d",&guess);
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
}while(guess!=magic);
printf(“counter=%d\n”,counter);
return 0;
}
运行结果显示:
在这里插入图片描述//通过函数time()读取计算机的时钟值(随机数种子设置)
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
main()
{
int magic,guess;
int counter=0;//设置计数
srand(time(NULL));
//unsigned int seed;
//printf(“please input a number:”) ;//随机数种子输入
//srand(seed);//设置不同的随机数种子,以产生不同的随机数序列
magic=rand()%100+1;//生成1到100的随机数
do{
printf(“Please input a magic number:”);
scanf("%d",&guess);
1counter++;
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
}while(guess!=magic);
printf(“counter=%d\n”,counter);
return 0;
}
//检测用户输入的数据是否正确 ,同时清除缓冲区非法字符
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
int magic,guess;
int counter=0;//设置计数
srand(time(NULL));
int ret;
magic=rand()%100+1;
do{
printf(“Please input a magic number:”);
ret=scanf("%d",&guess);
while(ret!=1);
{
while(getchar()!=’\n’)//缓冲区中非法字符清除
{
printf(“Please input a magic number:”);
ret=scanf("%d",&guess);
}
}
counter++;
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
}while(guess!=magic&&counter<10);
printf(“counter=%d\n”,counter);
return 0;
}
//检测用户输入的数据是否正确 ,同时清除缓冲区非法字符
//回答检测
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
int magic,guess;
int ret;
int counter=0;
char reply;
srand(time(NULL));
magic=rand()%100+1;
do{
do{
printf(“Please input a magic number:”);
ret=scanf("%d",&guess);
while(ret!=1);
{
while(getchar()!=’\n’);//缓冲区中非法字符清除
printf(“Please input a magic number:”);
ret=scanf("%d",&guess);
}//操作错误,重新输入
counter++;
if(guess>magic)
{
printf(“Wrong! too big!\n”);
}
else
if(guess<magic)
{
printf(“Wrong! too small!\n”);
}
else
if(guess=magic)
{
printf(“Right!\n”);
}
}while(guess!=magic&&counter<10);
printf(“counter=%d\n”,counter);
printf(“do you want to continue(Y/N or y/n)?”);
scanf("%c",&reply);
}while(reply==‘Y’ or reply==‘y’);
return 0;
}
作者:LJH@
来源:CSDN
原文:https://blog.csdn.net/CSDN447447LJH/article/details/90900773
版权声明:本文为博主原创文章,转载请附上博文链接!