猜数游戏C语言版(在Windows平台上使用)

74 阅读1分钟
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    // 生成一个 1 到 100 之间的随机数
    int number = rand() % 100 + 1;
    int guess, attempts = 0;

loop:
    printf("请输入你猜的数字:");
    scanf("%d", &guess);

    attempts++;

    if (guess > number) {
        printf("猜大了!请再试一次。\n");
        goto loop;
    } else if (guess < number) {
        printf("猜小了!请再试一次。\n");
        goto loop;
    } else {
        printf("恭喜你猜对了!你一共猜了%d 次。\n", attempts);
        // 使用 break 语句跳出循环
        break;
    }

    return 0;
}