开始有点难以理解了 do while也没完全学完
但相比速度快 我还是更愿意理解透彻 明天接着加油
(附上今天写的小游戏)
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void menu()
{
printf("************************************\n");
printf("******** 1.猜数字 ********\n");
printf("******** 0.退出 ********\n");
printf("************************************\n");
}
void game()
{
int y = 0;
int n = 0;
int ret = rand() % 100 + 1; //printf("%d\n", ret);
while (1)
{
printf("请猜一个数字\n");
scanf("%d", &n);
if (n < ret)
{
printf("猜小了\n");
y++;
}
else if (n > ret)
{
printf("猜大了\n");
y++;
}
else
{
printf("猜对啦\n");
printf("一共猜了:%d次\n", y);
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//定义随机数,其中需要用到#include<stdlib.h>和#include<time.h>,(unsign int)强制转换类型 int a = 0;和int *a = NULL;(空指针)一样的
do
{
menu();
printf("请选择\n");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}