利用c++写一个小游戏
时间: 2023-12-14 09:05:03 浏览: 25
好的,我可以为你提供一个简单的猜数字游戏的C程序示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int num, guess, count = 0;
srand(time(0)); // seed random number generator
num = rand() % 100 + 1; // generate random number between 1 and 100
printf("Welcome to the number guessing game!\n");
printf("I'm thinking of a number between 1 and 100.\n");
do
{
printf("Enter your guess: ");
scanf("%d", &guess);
count++;
if (guess > num)
{
printf("Too high! Try again.\n");
}
else if (guess < num)
{
printf("Too low! Try again.\n");
}
else
{
printf("Congratulations! You've guessed the number in %d attempts.\n", count);
}
} while (guess != num);
return 0;
}
这个程序会生成一个 1 到 100 之间的随机数,然后让玩家猜这个数。玩家每次猜数都会得到提示是猜得太高还是太低,直到猜中为止。程序还会统计玩家猜数的次数。