我如何避免在这里使用goto语句?

时间:2019-03-27 17:06:14

标签: c

我对使用C编程非常陌生,并且正在制作一个基于骰子的简单游戏。我所有其他逻辑都在工作,但是我在查看如何避免goto语句方面遇到麻烦。基本上,玩家以其余额为50美元开始,程序要求下注,然后掷骰子,具体取决于掷骰结果是什么。主要的问题是,结果出现时,系统会询问用户是否要再次玩游戏,如果他们拒绝,那么我就退出,但如果他们回答是,我需要重新开始并再次下注。


int main()
{

        START:

    if (FirstRun == 0)
    {
        printf("Your starting balance is $%.2f", Balance);
    }
    if (FirstRun > 0)
    {
        printf("Your new balance is $%.2f", Balance);
    }
    if (Balance <= 0)
    {
        printf("Sorry you are out of money!");
        exit(0);
    }

    do
    {
        ValidBet = 0;
        printf("\nPlease enter your bet:");
        scanf("%lf", &Bet);

        if ((Bet > Balance) || (Bet < 0))
        {
            printf("Your bet is invalid, try again.");
        }
        else 
        {
            ValidBet = 1;
        }
    } while (ValidBet == 0);

    ThrowDicePair();
    printf("DICE #1 WAS %d", SumOfThrows);

    if (SumOfThrows == 7 || SumOfThrows == 11)
    {
        Balance += Bet;
        printf("You win! Would you like to play again? [y/n]");
        C = getch();
        if (C == 'y')
        {
            FirstRun++;
            goto START;
        }
        else if (C == 'n')
        {
            printf("Thanks for playing");
            exit(0);
        }

}

3 个答案:

答案 0 :(得分:3)

只需将所有内容放入for(;;)循环中即可; goto 将是您重新循环的情况,在其他情况下,请添加return 0;(或您喜欢的exit(0);,并在某些情况下已经使用)

int main()
{

  for (;;) { /* ADDED  */
    if (FirstRun == 0)
    {
        printf("Your starting balance is $%.2f", Balance);
    }
    if (FirstRun > 0)
    {
        printf("Your new balance is $%.2f", Balance);
    }
    if (Balance <= 0)
    {
        printf("Sorry you are out of money!");
        exit(0);
    }

    do
    {
        ValidBet = 0;
        printf("\nPlease enter your bet:");
        scanf("%lf", &Bet);

        if ((Bet > Balance) || (Bet < 0))
        {
            printf("Your bet is invalid, try again.");
        }
        else 
        {
            ValidBet = 1;
        }
    } while (ValidBet == 0);

    ThrowDicePair();
    printf("DICE #1 WAS %d", SumOfThrows);

    if (SumOfThrows == 7 || SumOfThrows == 11)
    {
        Balance += Bet;
        printf("You win! Would you like to play again? [y/n]");
        C = getch();
        if (C == 'y')
        {
            FirstRun++;
            /* goto START; removed */
        }
        else if (C == 'n')
        {
            printf("Thanks for playing");
            exit(0);
        }
        else /* ADDED */
          return 0; /* ADDED */
    }
    else /* ADDED */
      return 0; /* ADDED */
  } /* ADDED  */
}

在更复杂/嵌套的情况下,您可以使用 continue 重新循环,但这在这里是无用的

答案 1 :(得分:2)

假设您要确保此人在选择退出之前至少玩过一次游戏,那么do ... while循环就可以了。只需在要迭代的地方开始循环即可,即您的START标签,然后在循环结束时将输入带入C中,条件是输入是否等于'n'或'N'。

答案 2 :(得分:0)

int main() {
    while (1) { /* ADDED  */
        if (FirstRun == 0) {
            printf("Your starting balance is $%.2f", Balance);
        }
        if (FirstRun > 0) {
            printf("Your new balance is $%.2f", Balance);
        }
        if (Balance <= 0) {
            printf("Sorry you are out of money!");
            exit(0);
        }

        do {
            ValidBet = 0;
            printf("\nPlease enter your bet:");
            scanf("%lf", &Bet);

            if ((Bet > Balance) || (Bet < 0)) {
                printf("Your bet is invalid, try again.");
            } else {
                ValidBet = 1;
            }
        } while (ValidBet == 0);

        ThrowDicePair();
        printf("DICE #1 WAS %d", SumOfThrows);

        if (SumOfThrows == 7 || SumOfThrows == 11) {
            Balance += Bet;
            printf("You win! Would you like to play again? [y/n]");
            C = getch();
            if (C == 'y') {
                FirstRun++;
                continue; /* ADDED  */
            } else if (C == 'n') {
                printf("Thanks for playing");
                exit(0);
            }
        }
        break; /* ADDED  */
    }
}