用C语言写一个切水果游戏代码
时间: 2023-12-29 12:25:10 浏览: 35
好的,下面是用C语言编写的切水果游戏代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int score = 0; // 初始化得分为0
char fruit[6][10] = {"apple", "banana", "orange", "peach", "watermelon", "pineapple"}; // 定义6种水果
srand(time(NULL)); // 设置随机数种子
while (1) {
int index = rand() % 6; // 随机选择一种水果
printf("Please cut the %s!\n", fruit[index]);
char input[10];
scanf("%s", input);
if (strcmp(input, fruit[index]) == 0) { // 如果输入正确,则得分加1
printf("Great! You got 1 point!\n");
score++;
} else { // 如果输入错误,则游戏结束,并输出最终得分
printf("Game over! Your score is %d.\n", score);
break;
}
}
return 0;
}
```
运行结果:
```
Please cut the peach!
peach
Great! You got 1 point!
Please cut the apple!
orange
Game over! Your score is 1.
```
在游戏中,程序会随机选择一种水果,然后提示玩家将该水果切开。如果玩家输入正确则得分加1,否则游戏结束并输出最终得分。