#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu() {
printf("************************\n");
printf("**1. play 0.exit ***\n");
printf("********************\n");
}
void game() {
int ret = 0;
int guess = 0;
ret = rand() % 100 + 1;/*运用时间戳*/
while (1) {
printf("请猜数字:>");
scanf_s("%d", &guess);
if (guess > ret){
printf("猜大了\n");
}
else if(guess<ret)
{
printf("猜小了\n");
}
else{
printf("恭喜猜对\n");
break;
}
}
printf("%d\n", ret);
}
int main() {
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请选择>:");
scanf_s("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
void是无类型;而menu则是菜单的意思,此代码运用了时间戳,同时运用了rand-随机数。其中运用了if和while循环语句,以及Switch选择语句,该编码可多次进行理解,对自身更有用。
1万+





