简单版本
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<Windows.h>
#include<string.h>
void menu()
{
char arr[] = "************************";
char arr1[] = "--Welcome to my game!!--";
int right = strlen(arr)-1;
int left = 0;
while (left <= right)
{
arr[left] = arr1[left];
arr[right] = arr1[right];
printf("%s\n", arr);
printf("************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("************************\n");
Sleep(300);
system("cls");
left++;
right--;
}
printf("--Welcome to my game!!--\n");
printf("************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("************************\n");
}
void game()
{
//srand((unsigned int)time(NULL));
int ret = rand() % 100 + 1;//用rand函数产生随机数需要用到srand
int guess = 0;
while (1)
{
printf("请猜数字:>");
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else {
printf("恭喜你,猜对啦!\\*v*/");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//(时间戳)NULL表示暂时并不需要这个参数,传一个空值(0)
//srand需要的返回值类型是unsigned int,time的返回值类型是time_t,所以我们需要同类型转化
//srand是设置随机数开始的起点
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();//猜数字的整个逻辑
break;
case 0:
printf("退出游戏!\\-_-/\n");
break;
default:
printf("输入错误,请重新输入:>");
break;
}
} while (input);
return 0;
}
标签:数字,int,srand,C语言,break,printf,input,include,游戏
From: https://blog.csdn.net/2203_75522143/article/details/140389339