虽然循环未能开始忽略if语句和条件 [英] While loop failing to begin ignoring if statements and conditions as a result

查看:146
本文介绍了虽然循环未能开始忽略if语句和条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个猜谜游戏的程序。然而,用于识别个人是否猜到正确数字的循环甚至无法开始我得到这个:

I'm working on a program that is a guessing game. However the loop for identifying whether an individual guessed the correct number fails to even begin I get this:

Player 1: Type a number between 0 and 99 and press return:
1
Type the number of guesses that player 2 gets and press return:
1
Sorry you are out of guesses. You lose.

循环在它开始之前终止,我无法弄清楚如何使其工作。

The loop terminates before it even begins and I can't figure out how to make it work.

这是循环的代码:

  printf( "Type the number of guesses that player 2 gets and press return: \n");
  scanf("%d",&guesses);

  while (remainingguesses != 0) {
    printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
    scanf(" %d",&secretnumberguess);

    if (secretnumberguess > secretnumber) {
      printf("Your guess was greater than the secret number.\n");
      remainingguesses = guesses - 1;
    }

    else if (secretnumberguess < secretnumber){
        printf("Your guess was less than the secret number.\n");
        remainingguesses = guesses - 1;
    }

    else{
          printf("Your guess was equal to the secret number. You win!\n");
    }
  }

  if (remainingguesses == 0)
    printf("Sorry you are out of guesses. You lose.\n");

我对编程很新,所以请原谅我的无知。

I'm fairly new to programming so excuse me for my ignorance.

以下是完整代码,如果您想看到:

#include <stdio.h>

int main()
{

  int secretnumber;
  int guesses;
  int secretnumberguess;
  int remainingguesses;

  while (1)  {
    printf("Player 1: Type a number between 0 and 99 and press return:\n");
    scanf(" %d",&secretnumber);

    if (secretnumber > 99 || secretnumber < 0) {
      printf("Secret number cannot be greater than 99 or below 0.\n");
      continue;
    }
    break;
  }

  printf( "Type the number of guesses that player 2 gets and press return: \n");
  scanf("%d",&guesses);

  while (remainingguesses != 0) {
    printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
    scanf(" %d",&secretnumberguess);

    if (secretnumberguess > secretnumber) {
      printf("Your guess was greater than the secret number.\n");
      remainingguesses = guesses - 1;
    }

    else if (secretnumberguess < secretnumber){
        printf("Your guess was less than the secret number.\n");
        remainingguesses = guesses - 1;
    }

    else{
          printf("Your guess was equal to the secret number. You win!\n");
    }
  }

  if (remainingguesses == 0)
    printf("Sorry you are out of guesses. You lose.\n");

  return 0;
}


推荐答案

你的剩余猜测导致你不进入循环。也许不是使用它作为检查使用标志并在循环内部检查if循环中的剩余猜测。在if循环内部相应地更改标志。

Your remainingguesses is causing you to not enter the loop. Perhaps instead of using that as a check use a flag and inside the loop check for remaining guesses in an if loop. And inside the if loop change the flag accordingly.

这篇关于虽然循环未能开始忽略if语句和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆