【问题标题】:"Guess The Number" In C But If Statements Don't Work RightC语言中的“猜数字”但如果语句不能正常工作
【发布时间】:2018-03-14 02:32:19
【问题描述】:

我有一个 C 语言程序,用户运行它来玩“猜数字”游戏。它可以正确运行以启动,但在用户输入 2 个数字(1 个初始数字和 1 个重试数字)后,程序在它应该有有限的尝试次数时重复。

这是我的程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    //----Guess a Number Game.-----------------------------------------
    // srand and rand needs the #include <stdlib.h> library

    srand(time(NULL)); //seeding the guess a number game with the system time, so the guess a # game always starts at a different point.
    int guess;
    int correctnum;
    correctnum = rand();

    printf("Enter a number:");
    scanf("%i",&guess);

    if(guess>correctnum)  // If aa is greater than bb AND aa is greater than cc.
    { 
      printf("Please enter another number, lower this time!");
      scanf("%i",&guess);
      main();
    }

    else if (guess<correctnum)
    {
      printf("Please enter another number, higher this time!");
      scanf("%i",&guess);
      main();
    }

    else if (guess==correctnum)
    {
      printf("You are a WINNER!\n");
      printf("You guessed the number right and it was %i!\n",correctnum);
    }
  int repeat;
  printf("Would you like to play again? 1=Yes and 2=No.");
  scanf("%i",&repeat);

  if(repeat==1)
  {
    main();
  }
  if(repeat==2)
  {
    printf("Hope you had a good time playing the game! See you soon!\n");
    return 0;
  }
}

【问题讨论】:

  • 不要递归调用main,使用循环
  • 至于你的问题,你应该运行多少“迭代”没有任何限制。
  • 你在调用main()之前使用scanf()并在函数顶部重复使用它
  • 每次再次调用 main 时,都会重新设置 rng。
  • @Someprogrammerdude 有没有什么方法可以重写我的程序的一部分,这样就不需要循环了,它只使用 if 语句?

标签: c function if-statement


【解决方案1】:

不要递归调用main()。你需要的是一个简单的循环。比如:

int main(void) {
    srand(time(NULL));
    int correctnum = rand();
    int guess;
    int done = 0;

    while (! done) {
        printf("Enter a number:");
        scanf("%i",&guess);

        if (guess>correctnum) { 
            printf("Please enter another number, lower this time!");
        } else if (guess<correctnum) {
            printf("Please enter another number, higher this time!");
        } else /* if (guess==correctnum) */ {
            printf("You are a WINNER!\n");
            printf("You guessed the number right and it was %i!\n",correctnum);

            done = 1;
        }
    }
}

您可能也应该检查 scanf() 中的错误,但首先要做的是。

【讨论】:

  • 在您的示例中,每个循环使用 scanf() 2 次​​span>
  • main() { ... } 自 1999 年以来一直不是有效的 C。
【解决方案2】:

你可以在一个循环中实现所有你想要的功能:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void main(){
    srand(time(NULL));
    int guess;
    int correctnum;
    correctnum = rand() % 100;  #it's better to get a number between 1~100
    int done = 1;

    printf("guess the number:\t");
    scanf("%i", &guess);

    while(done){
        if(guess < correctnum){
            printf("Please enter another number, higher this time:\t");
            scanf("%i", &guess);    
        }

        else if(guess > correctnum){
            printf("Please enter another number, lower this time:\t");
            scanf("%i", &guess);
        }

        else if(guess == correctnum){
            printf("well done! if you want to play again, input 1, else input 0:\t");
            scanf("%i", &done);
            if(done == 1){
                correctnum = rand() % 100;
                printf("guess the number:\t");
                scanf("%i", &guess);
            }
        }
    }

    printf("Hope you had a good time playing the game! See you soon!\n");
}

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多