【问题标题】:Problems with bool variables within a if else comparatorif else 比较器中的布尔变量问题
【发布时间】: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


【解决方案1】:

在与if (choice == 1) 关联的大括号内写入bool weapon1 = true; 定义了一个与之前的weapon1 无关的新weapon1

将其更改为weapon1 = true;,它分配给早期的weapon1,而不定义新的weapon1

在编译器中打开所有或大部分警告消息并注意它们。

【讨论】:

  • UV 最重要的部分“打开编译器中的所有或大多数警告消息”。
【解决方案2】:

问题在于您重新初始化了武器布尔变量。 正确的代码是这样的:

#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");
    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;
}

【讨论】:

  • 原代码创建了一个新变量,也称为weapon,它不会重新初始化之前的武器
【解决方案3】:

在 C 中使用 goto(有一些例外情况会出现在函数末尾)被认为是一种可怕的做法

当你定义了变量后,你不能在赋值开始时使用它的类型作为它的新定义。而是bool weapon1 = true;weapon1 = true;

#include <stdbool.h>
#include <stdio.h>

int main() 
{
    int choice;
    int choice2;
    bool weapon1 = false;
    bool endgame = false;

    do
    {
        printf("Where would you like to go? 1. Store  2. Arena: ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("You got weapon1 from the store \n");
                weapon1 = true;
                break;
            case 2:
                if (weapon1) printf("You won the battle because you had a weapon\n");
                else  printf("You lost the battle because you did not have a weapon\n");
                endgame = true;
                break;
            default: 
                break;                
        }
    }while(!endgame);
}

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多