【问题标题】:Simple guessing game that allows user to play again after correctly guessing the random number?简单的猜谜游戏,允许用户在正确猜出随机数后再次玩?
【发布时间】:2016-03-12 05:06:36
【问题描述】:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

int random_num;
void guessGame(int guessed_num){

    char answer;
if (guessed_num == random_num){
    printf("Correct! That's the number.\n");
    printf("Would you like to play again (y or n)?\n");
    scanf("%c", &answer);
    if (answer=='y')
        scanf("%d", &guessed_num);
        guessGame(guessed_num);
    if (answer=='n')
        return 0;


}
else
    if (guessed_num < random_num)
        printf("Too low. Guess again.\n");
    else
        printf("Too high. Guess again.\n");
}

int main(){

int guessed_num = 0;
srand(time(NULL));
random_num = rand() % 50 + 1;

printf("I have a number between 1-50.\n");
printf("Can you guess what it is?\n");
printf("Enter your initial guess.\n");

while(1){

    scanf("%d", &guessed_num);
    guessGame(guessed_num);
}

return 0;
}

所以在用户找到正确的随机数后,我希望他们能够再次玩游戏(如果他们愿意的话)。如何让代码重新运行?我尝试在我的guessGame 函数中这样做,但我知道这是错误的。

【问题讨论】:

    标签: c function random


    【解决方案1】:

    由于我的英语不好,我会给你代码(根据你的修改)。

        #include<stdio.h>
        #include<stdio.h>
        #include<stdlib.h>
        #include<math.h>
        #include<time.h>
    
        int random_num;
    
        int guessGame(int guessed_num) {
    
    
            if (guessed_num == random_num) {
               printf("Correct! That's the number.\n");
               return 1;
            } else if (guessed_num < random_num) {
                  printf("Too low. Guess again.\n");
                  return 0;
               } else {
                  printf("Too high. Guess again.\n");
                 return 0;
            }
        }
    
        int main() {
        char loop;
        while (1) {
        int guessed_num = 0;
        srand(time(NULL));
        random_num = rand() % 50 + 1;
        printf("I have a number between 1-50.\n");
        printf("Can you guess what it is?\n");
        printf("Enter your initial guess.\n");
        while (1) {
            scanf("%d", &guessed_num);
            int returnValue = guessGame(guessed_num);
            if (returnValue == 1) {
                break;
            }
        }
        printf("You wanna do it again? ");
        getchar();
        scanf("%c", &loop);
        if (loop == 'n') {
            break;
        }
    }
    return 0;
    } 
    

    【讨论】:

      【解决方案2】:

      我看到的最简单的方法是让guessGame 返回一个布尔值。如果他们做对了,那就是真的,如果他们做错了,那就是假的。然后在你的 main 中,有另一个 bool 变量,假设为 guessedRight,将其设置为 guessGame 的返回值,并将其用作 while 循环条件。

      也许更简单的方法是将您从用户那里得到猜测的行移到guessGame 函数中(然后它不需要任何参数)。那么您再次调用 geussGame 的尝试应该会奏效。

      所以选项1:编辑guessGame的开头

      void guessGame(){
          int guessed_num = 0;
          scanf("%d", &guessed_num);
      

      然后完全删除

      if (answer = 'n')
        return 0;
      

      它没有任何作用,而且你的函数无论如何都是一个 void 返回类型。然后在 main 中调用guessGame。这种方法也不需要 while 循环。

      选项2:更改guessGame的返回类型

      bool guessGame(){
           int guessed_num = 0;
           scanf("%d", &guessed_num);
      

      扫描,得到答案,然后

      if (answer = 'y')
          return true;
      
      return false
      

      那么在main中,我认为你实际上可以这样做

      while(guessGame()) {}
      

      【讨论】:

      • 嘿,感谢您的帮助。我实际上编辑了我的问题。我的意思是:“简单的猜谜游戏,让用户猜对随机数后可以再次玩?”
      • @abhishek_naik 嘿那里
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多