2013-04-20 76 views
0

任何人都可以告訴我,爲什麼我的代碼工作正常,直到我到最後的不足,我問用戶他們是否想再次玩?出於某種原因,該程序似乎忽略了這一行代碼。請儘量溫柔,因爲我是編程新手,並試圖自學Objective-C。這個程序對於noobs是典型的,我產生一個隨機數字,要求用戶猜測,然後詢問他們是否想再次玩。謝謝。爲什麼我的最終scanf停止並閱讀用戶輸入?

#import <Foundation/Foundation.h> 

int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 

     int randomNumber = arc4random_uniform(100); // This is a random number generator that gens a num betw 0 and 100 
     int userNumber;  // This is the number that the user picks intially 
     int attempts = 0; // This is the number of attempts the user makes during each game 
     int games = 0;  // This is the number of games the user has played 
     char play = 'n'; // This is whether the user wants to play again, intially set to 'y' 

     scanf("%c", &play); 
     while (play == 'y') { 

      NSLog(@"Random number is: %d", randomNumber); 
      NSLog(@"Enter a number between 0 and 100"); 
      scanf("%d", &userNumber); 
      games++; // Increment the number of games the user has played to 1 

      if (userNumber == randomNumber) { 
       attempts++; 
       NSLog(@"Congratulations. You guessed correctly!"); 
      } 

      attempts++; 

      while (userNumber != randomNumber) { 

       if (userNumber < randomNumber) { // Guess is too low 
        attempts++;      // attempt is incremented 
        NSLog(@"Too low. Try again!"); // User tries again 
        scanf("%d", &userNumber); 
       } 

       if (userNumber > randomNumber) { // Guess is too high 
        attempts++;      // attempt is incremented 
        NSLog(@"Too high. Try again!"); // User tries again 
        scanf("%d", &userNumber); 
       } 
      } 
      NSLog(@"Congratulations. You guessed correctly!"); 
      NSLog(@"It took you %d attempts to guess correctly", attempts); 


      NSLog(@"Do you want to play again?"); 
      scanf("%c", &play); // --------- Here is where things to wrong --------- 

     } // while play is yes 

    } // autoreleasepool 
    return 0; 
} // main 
+0

這真的沒有任何用Objective-C做什麼;當'printf'執行相同操作時,你正在使用'NSLog'。可能值得作爲一個簡單的C問題重新標記。 – 2013-04-20 21:46:04

+0

可能最後的'scanf()'讀取換行符並繼續(數字不讀取換行符)。也許在'%c'之前放一個空格:'scanf(「%c」,&play);'。檢查'scanf()'的返回值,甚至可能檢查讀取的字符 – 2013-04-20 21:55:46

+0

%c如何做到這一點?我認爲它是讀取/ n字符而不是我想要它讀取的內容,它是'y'或'n'。就我的理解而言,%d整數沒有閱讀換行符,但是%c的確是這樣的嗎?是否正確?防止這種情況的方法是使用空格嗎?我只是不明白我會怎麼做到這一點。謝謝。 – 2013-04-20 22:24:02

回答

1

轉換註釋到答案:

也許,最終scanf()讀取換行,並繼續(數字不讀行)。也許把一張空白的%c前:

scanf(" %c", &play); 

檢查返回值從scanf(),也許連查的字符被讀取。

還擊:

%c前空間的伎倆。人們如何學習這樣的事情?我認爲它是讀取\n字符,而不是我想要讀取的字符,它是'y'或'n'。據我的理解,%d整數不讀在換行符中,但是%c呢?那是對的嗎?而防止這種情況的方法是使用空間?我只是不明白我會怎麼知道要這樣做。

和響應:

通過非常仔細地閱讀手冊頁scanf(),過多次,或痛苦的經歷(或通過SO回答很多問題它)。 scanf()功能家族功能非常強大,準確使用非常困難。我一般推薦使用fgets()讀取輸入的行:

char line[4096]; 
if (fgets(line, sizeof(line), stdin) != 0) 
{ 
    ...use line... 
} 

sscanf()組合來解析就行了數據。它通常會導致您遇到的那種驚喜減少。您應該始終檢查scanf()是否按照您的預期進行了多次轉換。

白色空間在scanf()中的作用 - 家庭格式字符串錯綜複雜。大多數轉換說明符會自動跳過前導空格(包括換行符),因此格式字符串"%d%d"將讀取爲整數值,其中第一個可能前面有任意數量的空格,第二個可能前面有任意數量的空間。轉換將停止在不能成爲第二個整數部分的第一個字符處(除非先前有錯誤)。如果輸入8和換行符作爲輸入,則轉換停止在換行符(\n)上,如果下一個輸入要讀取則轉換結束。

數值轉換和字符串轉換%s都跳過前導空白。單字符格式(%c)和掃描集%[a-z]不會跳過前導空格。

當格式中出現空白字符時(如"%d %c"),則表示數據中的任意數量的空白,包括零。因此,在以下各行中,變量接受%c格式將獲得Z每次:(最後兩行是最後輸入一起讀)

123Z 
123 Z 
123  Z 
123 
       Z