代码之家  ›  专栏  ›  技术社区  ›  Djeter277

随机数游戏C

  •  0
  • Djeter277  · 技术社区  · 12 年前

    虽然我第一次玩这个游戏很顺利;第二次它只给你两条生命。。。我已经试图改变生命的数量,但仍然无法弄清楚我做错了什么。

    //C_program_random_number_game

    #include<stdio.h>
    #include<time.h>
    #include <stdlib.h>
    
    int main()
    {
    
       srand(time(NULL));
       int num1,x = 0;
       char game, cont, replay;
    
       printf("Would you like to play a game? : ");
       scanf("%c",&game);
    
      if (game == 'y' || game == 'Y')
      {
        printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
        do
        {
            int r = rand()%5 +1;
            printf("\n\nEnter a number between 1 and 5 : ");
            scanf("\n%d",&num1);
            x++;
            if(num1 > 0 && num1 < 5)  
            {
    
                do
                {
                 if(num1 < r)
                    {
                        printf("\nClose! try a little higher... : ");
                        x++; 
                    }
                  else if (num1 > r)
                    {
                        printf("\nClose! try a little lower...: ");
                        x++; 
                    }
                    scanf("%d",&num1);
    
                }while(num1!=r && x <3); 
    
                if(num1 == r) 
                {
                    printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
                }
                else if(num1 != r)
                {
                    printf("\nBetter luck next time!");
                }
                printf("\n\nWould you like to play again? (y or n) : ");
                scanf("\n%c",&replay);
            }
                else
            {
                printf("Sorry! Try again : ");
                scanf("%d",&num1);
            }
    
        }while(replay == 'y'|| replay == 'Y');
    
    }
    else if (game == 'n' || game == 'N')
    {
        printf("Okay, maybe next time! ");
    }
    else
    {
        printf("Sorry, invalid data! ");
    }
    return 0;
    

    }

    2 回复  |  直到 12 年前
        1
  •  1
  •   MrHappyAsthma    12 年前

    你的代码有各种各样的问题(大多数都是编程方面的小问题)。大多数错误都是你想通过这个问题做什么以及你打印什么()中的错别字。

    实际上,这段代码将在1-25之间随机,接受任何有效int的输入,看看是否匹配,然后只给你5次尝试。(我没有添加错误检查来强制输入在1-25之间。这可能应该加上。)

    我在下面的代码中添加了所有的更改,并按照printf()中的内容进行了修改。

    注:由于我已经指出了我的更改,请参见我上面的评论。我还对它进行了格式化,使其更易于阅读。

    注2:我使用在线编译器快速完成了这一操作。如果你发现这有什么问题或不按你的意愿工作,请在下面评论,我会解决它。

    // C_program_random_number_game
    
    #include<stdio.h>
    #include<time.h>
    #include <stdlib.h>
    
    int main()
    {
    
        srand(time(NULL));
        int num1,x = 0;
        char game, cont, replay;
    
        printf("Would you like to play a game? : ");
        scanf("%c",&game);
    
        if (game == 'y' || game == 'Y')
        {
            printf("\nThe rules are simple. You have have 5 tries to guess the                 computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
    
            do
            {
                int r = rand()%25 +1;
    
                printf("\n\nEnter a number between 1 and 25 : ");
                scanf("%d",&num1);
    
                do
                {
                    printf("r = %d\n", r);
    
                    if(num1 < r)
                    {
                        printf("\nClose! try a little higher... : ");
                        x++; //Increment x if wrong guess
                    }
                    else if (num1 > r)
                    {
                        printf("\nClose! try a little lower...: ");
                        x++; //Increment x if wrong guess
                    }
    
                    scanf("%d",&num1);
                }while(num1!=r && x < 5); //If x is 5 or more, they ran out of guesses (also, you want an && not an ||)
    
               if(num1 == r) //Only print "winner" if they won!
               {
                   printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
               }
    
                printf("\nWould you like to play again? (y or n) : ");
                scanf("\n%c",&replay);
            }while(replay == 'y'|| replay == 'Y');
        }
    
        printf("Thanks for playing! ");
    
        if (game == 'n' || game == 'N')
        {
            printf("Okay, maybe next time! ");
        }
        return 0;
    }
    
        2
  •  1
  •   syncsynchalt    12 年前

    有两个问题。第一个是,当数字匹配时,您不会跳出“for”循环。因此,比赛只在每三次猜测时进行检查。

    第二个问题是逻辑检查:

    }while(num1!=r || x <= 3);
    

    我们看到,如果for循环提前中断,这将变成“真”。