使用c编写猜数字小游戏

void func_guess_number(){
int guess, magic, ret, counter;
counter = 0;
srand(time(NULL));
magic = rand()%100+1;
printf("generate magic is %d.\n", magic);

do{
printf("Please input a number: \n");
ret = scanf("%d", &guess); // 如果输入的是字符就让他继续输入
if (ret != 1){
while (getchar() != '\n'){;}
printf("please input a digital number\n");
continue;
}

counter++;
if (magic > guess){
printf("Wrong, too low.\n");
}
else if (magic < guess){
printf("Wrong, too high.\n");
}
else{
printf("guess right.\n");
}
fflush(stdin);
} while (magic != guess);

printf("guess times is %d.\n", counter);
}
posted @ 2021-11-21 17:05  一只小麻瓜  阅读(281)  评论(0编辑  收藏  举报