猜数字编程c语音怎么写

时间:2025-03-04 01:19:24 明星趣事

```c

include

include

include

int main() {

int target, guess, attempts = 0;

// 生成随机数作为目标数字

srand(time(0));

target = rand() % 100 + 1;

printf("欢迎来到猜数字小游戏!\n");

printf("游戏规则:请猜一个1到100之间的数字\n");

do {

printf("请输入你的猜测:>");

scanf("%d", &guess);

attempts++;

if (guess > target) {

printf("你猜的数字太大了,请再试一次。\n");

} else if (guess < target) {

printf("恭喜你猜对了!你用了 %d 次猜对了数字 %d。\n", attempts, target);

}

} while (guess != target);

return 0;

}

```

这个程序首先生成一个1到100之间的随机数作为目标数字,然后提示用户输入猜测。根据用户的输入,程序会给出相应的提示(猜大了、猜小了或猜对了),直到用户猜中为止。程序还会记录用户猜中的次数,并在猜中后显示出来。