【发布时间】:2020-08-06 20:15:32
【问题描述】:
我正在尝试在我的游戏中制作升级系统。我正在使用 3 个布尔变量(用于 3 种不同的武器)。我无法制作一个 if else 比较器来查看哪些布尔变量是真/假(您拥有和没有哪些武器),并带您进入适当的战斗场景。无论您拥有什么武器,if else 比较器都不起作用。它会自动转到语句的“其他”部分(如果你没有武器,你会去哪里)。我已经包含了一个简化版本,只有 1 个布尔变量。请帮忙。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
int choice2;
bool weapon1 = false;
start:
printf("Where would you like to go? 1. Store 2. Arena: ");
scanf("%d", &choice);
if (choice == 1) {
printf("You got weapon1 from the store \n");
bool weapon1 = true;
goto start;
}
else if (choice == 2) {
printf("You went to the arena\n");
if (weapon1) {
printf("You won the battle because you had a weapon\n");
exit(0);
}
else {
printf("You lost the battle because you did not have a weapon\n");
exit(0);
}
}
return 0;
}
【问题讨论】:
-
将
bool weapon1 = true;更改为仅weapon1 = true;您正在choice == 1范围内定义一个新的weapon1...但您想要main范围内的weapon1变量跨度> -
您在 if 语句中重新定义武器 1,使其位于该代码块的本地,删除 'bool'
标签: c if-statement boolean