2017-06-25 129 views
-3

我需要帮助完成我的程序。我似乎无法得到最后一步,当用户输入'y'来玩另一个游戏时,它应该重新开始。如果用户输入'n'那么它应该结束。预先感谢您的任何帮助。以下是我到目前为止的代码。C编程猜测游戏

问题出在这里:
编写一个C程序,与用户一起玩猜数字游戏。

下面是示例运行:

OK, I am thinking of a number. Try to guess it. 

Your guess? 50 
Too high! 
Your guess? 250 
Illegal guess. Your guess must be between 1 and 200. 
Try again. Your guess? 30 
**** CORRECT **** 
Want to play again? y 
Ok, I am thinking of a number. Try to guess it. 
Your guess? 58 
**** CORRECT *** 
Want to play again? n 
Goodbye, It was fun. Play again soon. 

代码:

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

int main() { 
    int i, number, currentGuess, MAX_GUESS = 5; 
    int answer = 'n'; 
    time_t t; 

    srand((unsigned) time(&t)); 
    number = rand() % 200 + 1; 

    printf("Welcome to the game of Guess It! \nI will choose a number between 1 and 200. \nYou will try to guess that number."); 
    printf("If you guess wrong, I will tell you if you guessed too high or too low. \nYou have 5 tries to get the number. \n"); 
    printf("\nOK, I am thinking of a number. Try to guess it. \n"); 

    for (i = 0; i < MAX_GUESS; i++) { 
     printf("\nYour guess?"); 
     scanf("%i", &currentGuess); 

     if (currentGuess > 200) { 
      printf("Illegal guess. Your guess must be between 1 and 200.\n"); 
      printf("Try again.\n "); 
     } 
     else if (currentGuess > number) { 
      printf("Too high!\n"); 
     } 
     else if (currentGuess < number) { 
      printf("Too low!\n"); 
     } 
     else { 
      printf("****CORRECT****\n"); 
      return 0; 
     } 
    } 

    printf("Sorry you have entered the maximum number of tries. \n"); 
    printf("Want to play again? \n"); 
    scanf("%i", &answer); 

    if(answer == 'n') { 
     printf("Goodbye, it was fun. Play again soon.\n"); 
     return 0; 
    } 
    else if (answer != 'n') { 
     printf("Ok, I am thinking of a number. Try to guess it.\n"); 
    } 
} 
+0

将您的代码导出到其他方法。 int game(){....}和main()只是调用game()。然后当你想让用户重新启动时再次调用game() – Pentarex

+0

你应该看看https://stackoverflow.com/questions/44742209/im-trying-to-code-a-number-guessing-game。看来你们都试图解决类似的编码问题。 –

+0

然后看看'man scanf'并考虑'scanf(“%i”,&answer);'和if(answer =='n')'。具体是什么问题可能是。当你输入'n'作为答案时,'scanf'认为'n'是什么? –

回答

-1

这里有一个想法,让你在你的代码做到这一点:

char answer = 'y'; 

do 
{ 
    // ... 
    printf("Want to play again (y/n)? "); 
    scanf(" %c", &answer); 
    // Mind^the space here to discard any 
    // previously read newline character 

} while (answer == 'y'); 
+0

谢谢Azeem,我会试试看。 – lizmart

+0

@lizmart:不客气! :) – Azeem

+1

...嗯,不。你需要检查'scanf'的返回值。如果用户按下Ctrl + D/Ctrl + Z,则假定为“y”。同样,你也不会丢弃答案中的额外空白,所以如果例如新行仍然是未读的,那么它将被这个消耗,并且你得到消息两次... –

0

检查下面的代码,你可能想理解代码,而不是仅仅粘贴它。

  1. 该代码将执行重复性任务的任务委托给函数。

  2. 然后代码依赖函数来说出玩家玩过多少次机会,或者-1表示成功。

  3. 并注意getchar()而不是scanf

注:额外的getchar()是必要的,因为我不知道,但考虑字符输入取整数输入后提出的一个stdin性格'\n',这会导致不必要的行为做。所以这是我总是用来解决这种情况的一个小窍门。

  • ,请注意do while循环,这种情况下成为一个典型的例子,其中,应使用do while循环。

  • 另外我用宏来定义MAX_GUESS。这是一个很好的做法。你可能想谷歌它“为什么?”

  • 如果您有任何疑问,请在答案中留言。

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    #include <ctype.h> 
    
    #define MAX_GUESS 5 
    
    int playGame(int number) 
    { 
        printf("\nOK, I am thinking of a number. Try to guess it. \n"); 
    
        int i, currentGuess; 
    
        for (i=0; i<MAX_GUESS; i++){ 
    
         printf("\nYour guess?: "); 
         scanf("%i", &currentGuess); 
    
         if (currentGuess > 200) { 
          printf("Illegal guess. Your guess must be between 1 and 200.\n"); 
          printf("Try again.\n "); 
         } 
    
         else if (currentGuess > number) { 
           printf("Too high!\n"); 
         } 
    
    
         else if (currentGuess < number) { 
          printf("Too low!\n"); 
    
         } 
    
         else { 
          printf("****CORRECT****\n"); 
          return 0; 
         } 
        } 
    
        return i; 
    } 
    
    int main() { 
    
    
        int i, number, x; 
    
        char answer = 'n'; 
    
        time_t t; 
    
        srand((unsigned)time(&t)); 
    
        printf("Welcome to the game of Guess It! \nI will choose a number between 1 and 200. \nYou will try to guess that number."); 
        printf("If you guess wrong, I will tell you if you guessed too high or too low. \nYou have 5 tries to get the number. \n"); 
    
        do 
        { 
         number = rand()%200+1; 
         x = playGame(number); 
    
         if (x == MAX_GUESS) 
          printf("Sorry you have entered the maximum number of tries. \n"); 
    
         printf("Want to play again? \n"); 
    
         // To flush the newline character 
         getchar(); 
         answer = getchar(); 
    
        } while (answer != 'n' || answer != 'N'); 
    
        printf("Goodbye, it was fun. Play again soon.\n"); 
    
        return 0; 
    } 
    
    +0

    谢谢!因为我们的老师或书没有提到它,所以我没有想到while循环。我只是在我的书中重新读了getchar(),是的,这更有意义! – lizmart

    +0

    @lizmart如果答案有帮助,您可以接受答案。 –