2017-06-25 126 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如果答案有幫助,您可以接受答案。 –