写了一个猜数字的小游戏,虽然写之前老师讲过了一遍,但是我还是想了好久,琢磨琢磨,弄了一下午,还很勉强.
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int menu() {
printf("-------------------------------------------\n");
printf("-----------------1.开始游戏----------------\n");
printf("-----------------0.退出游戏----------------\n");
printf("-------------------------------------------\n");
printf("请输入您的选择:");
int i;
scanf("%d", &i);
return i;
}
int game() {
srand((unsigned int)time(0));
int to_gass = rand() % 100 + 1;
int num = 0;
while (1) {
printf("请输入一个数字:");
scanf("%d", &num);
if (num < to_gass) {
printf("低了\n");
}
else if (num > to_gass) {
printf("高了\n");
}
else {
printf("恭喜你,猜对了!\n");
break;
}
}
}
int main() {
while (1) {
again:
int choice = menu();
if (choice == 1) {
game();
}
else if (choice == 0) {
break;
}
else
printf("您的输入有误,请重新输入:\n");
goto again;
}
system("pause");
return 0;
}