编程如下:
#include
#include
#include
int main(int argc, char * argv[])
{
srand((unsigned)time(NULL))
int a = rand()%100
a+=1
int b,c
c = 0
while (c {
printf("Please enter a number: ")
if (scanf("%d", &b) == 0)
{
fprintf(stderr, "Invalid entry.\n")
return EXIT_FAILURE
}
else
{
if (b == a)
{
printf("You are correct, the number is %d!\n", a)
printf("You used %d times to get the answer.\n", c)
break
}
else if (b >a)
printf("The number is bigger than it suppose to.\n")
else
printf("The number is smaller than it suppose to.\n")
c++
}
if (c == 10)
{
printf("You used 10 times, please try again!\n")
break
}
}
return EXIT_SUCCESS
}
编程的注意事项:
1、程序不仅需要给计算机读 , 也要给程序员读。 程序设计风格的原则 , 代码应该清楚的和简单的 , 具有直截了当的逻辑 , 自然的表达式 , 通行的语言使用方式 , 有意义的名字和帮助作用和注释。
2、自定义类型名以大写字母开头,各单词之间以大写字母分隔,如 CallType (即骆驼式命 名法) 。 变量名以小写字母开头, 各单词之间以大写字母 分隔 (变量活动范围前缀以下划线 分隔) ,如 m_pReleaseIn。函数名以大写字母开头,各单词之间以大写字母分隔(进程、进 程页及子函数前缀以下划 线分隔) ,如 Sub_ErrorDealing。
3、命名宏定义时,表示最大个数时定义为 XXX_MAX_NUM(如最大子节点个数可用 SNODE_MAX_NUM表示) ,表示最大取值时定义为 XXX_MAX(如 PT 板 E1的最大取值 可用 PT_E1_MAX表示) 。定义最小个数时定义为 XXX_MIN_NUM,定义最小取值时定义 为 XXX_MIN。 (以防止下标使用时难以分辨是否需要减 1) 。
//小游戏:猜数字 随机一个1-100之间的数,根据数据输入进行提示#include <stdlib.h>
#include <time.h>
int main(void){
int value=0
int num=0
srand((unsigned int) time(NULL))
num=rand()%100+1//1-100
while(1){
scanf("%d",&value)
if(num>value){
printf("您猜小了\n")
}
else if(num<value){
printf("您猜大了\n")
}
else if(num=value){
printf("恭喜您猜对了\n")
break
}
}
return 0
}
#include <stdio.h>#include <stdlib.h>
void clear()
{
scanf("%*[^\n]")
scanf("%*c")
} //清除输入缓冲区
void Inspect(int number,int cn)
{
int innum,flag=0
while(cn<7 &&flag==0){
printf("输入猜测的数据(0~99):")
scanf("%d",&innum)
if(innum==number)
{
flag=1
printf("恭喜,猜对了,共用了%d次!\n",cn)
}
else if(innum>number)
{
cn++
printf("很遗憾,你猜错了,是更小的数,你还有%d次机会!\n",7-cn)
}
else
{
cn++
printf("很遗憾,你猜错了,是更大的数,你还有%d次机会!\n",7-cn)
}
}
if(cn==7){
printf("你已经没有机会尝试了,请重新再来\n")
printf("%d\n",number)
}
} //游戏主体判断函数
int main()
{
int number=rand()%100
int cn=0
char tp='y'
Inspect(number,cn)
while(tp=='y'){
printf("你还想继续尝试吗?(y/n):")
clear() //清除输入缓冲区,防止之前的输入影响后面的代码执行
scanf("%c",&tp)
if(tp=='y') {cn=0Inspect(number,cn)}
}
return 0
}