c - 在使用 'IF' 语句前进后,有没有办法返回?

标签 c

所以我正在为我的“C”编程类(class)制作这个基于文本的游戏。 即使在我完成类(class)后我也想继续它,所以这不仅仅是一个家庭作业,我实际上想保留它以备后用。

我的问题是,在这部分代码中,是否有一种方法可以让用户回到上一个使用“IF”语句时提出的问题,以便他们可以在最终走向故事的方向之前做出多项选择?

if (door2 == 1)
{
    // what happens when you touch the orb.
    printf("The orb transports you to an unknown location.\n");
    printf("You look around to get the lay of your area,\n");
    printf("off in the distance you see a church like building.");
    door2 = door2 - 1;
    printf("\n");
    printf("Do you go to the building? ");
    scanf_s("%d", &door1);
    printf("\n");
    if (door1 ==  1)
    {
        // Going up to the church.
        printf("You walk up to the building, it gives you a strange feeling\n");
        printf("in the pit of your stomach. You look around and see two windows\n");
        printf("low enough for you to see into along with the main door.\n");
        printf("Do you go to the left window? right window? or try to open the front door?\n");
        scanf_s("%d", &movement);
        printf("\n");
        if (movement = 1)
        {
            // left window.
            printf("You creep up to the window and peak inside...\n");
            printf("You see a large figure pacing back and forth in the room\n");
            printf("what ever it is spots you and darts further into the church.");
            movement = movement - 1;
            // i subtract the L,S,R numbers to insure there is not an infinite loop.
        }
        else if (movement = 2)
        {
            // front door.
            printf("You walk up to the front door, and try to open it.\n");
            printf("The door does not budge, it must be locked.");
            movement = movement - 2;
        }
        else if (movement = 3)
        {
            // right window.
            printf("You creep up to the window and find an empty room.\n");
            movement = movement - 3;
            printf("The window is cracked open somewhat... Do you try to enter the\n");
            printf("church through the window? ");
            scanf_s("%d", &door2);
            printf("\n");
        }

最佳答案

实际上是的,如果你只是围绕它放置一个循环,但我建议在函数中拆分你的 ifs。您的代码看起来会好得多。

void main() {
  enterTheCurch();
}

void enterTheCurch() {
  printf("You've entered the Curch. There are 2 Doors. Do you want to go left or right?");
  String direction;
  scanf_s("%s", &direction);
  if(direction == "left") {
    goLeftCurchDoor();
  }
  else {
    goRightCurchDoor();
  }
}

goLeftCurchDoor() {
  ...
}

goRightCurchDoor() {
  ...
}

尝试使用这个结构。像这样,您还可以为 bool alreadyVisited 之类的函数提供参数,并根据这些参数更改打印件。

关于c - 在使用 'IF' 语句前进后,有没有办法返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033455/

相关文章:

c - pic32中的文件处理

c - 同一内存位置上的结构和数组声明

c - 拆分一个字符串并打印出每个单词

c - 尝试将字符串传递给函数时出现错误段错误(核心已转储)

c - 使用不同功能时出现段错误

c - 在 C 中获取段错误

c - C 中的声明错误终止(大括号)

c - fopen 无法正常工作,当我使用 fclose 时出现段错误

c++ - 用于测试目的 : which floating point (IEEE754 32b) numbers are "special"?

c - 为什么这个函数会导致段错误?