C语言编程 如何构建一个简单的猜数字小游戏
源代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>//生成随机函数起点时用到time.h
int main()
{
int number1 = 0, choice = 0, number2 = 0;
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");//界面设置
printf("$$$$$ 欢 迎 来 到 $$$$$\n");
printf("$$$$$ 猜 数 字 游 戏 $$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf(" 1.进入游戏\n");
printf(" 2.退出游戏\n");
scanf("%d", &choice);
srand((unsigned int)time(NULL));//设置随机数起点
while (choice == 1)
{
printf("请输入所猜的数:\n");
scanf("%d", &number2);
number1 = rand() % 100;
while (1)
{
if (number2 == number1)
{
printf("666 恭喜你答对了!");
printf("\n1.再玩一次\n2.退出游戏\n");
scanf("%d", &number2);
if (number2 == 1)
break;
if (number2 == 2)
return 0;
}
if (number2 < number1)
{
printf("\n猜小了!\n");
scanf("%d", &number2);
}
if (number2 > number1)
{
printf("\n猜大了!\n");
scanf("%d", &number2);
}
}
}
while(choice == 2)
{
return 0;
}
system("pause");
return 0;
}
运行结果: