簡體   English   中英

為什么我的最終scanf不會停止並讀取用戶輸入?

[英]Why doesn't my final scanf stop and read user input?

誰能告訴我為什么我的代碼在我進入最后決賽之前會正常工作,我問用戶是否願意再次玩嗎? 由於某種原因,該程序似乎忽略了這一行代碼。 請保持謙虛,因為我是編程和嘗試自學Objective-C的新手。 這個程序是典型的菜鳥,我會生成一個隨機數,讓用戶猜測,然后問他們是否想再次玩。 謝謝。

#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

將評論轉換為答案:

最終的scanf()會讀取換行符並繼續(數字不會讀取換行符)。 也許在%c之前加一個空格:

 scanf(" %c", &play); 

檢查scanf()的返回值,甚至可以檢查讀取了哪個字符。

Riposte:

%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 )和掃描集%[az]不會跳過前導空白。

當空格字符以"%d %c"的格式出現時,則表示數據中任意數量的空格,包括零。 因此,在以下每一行中,接收%c格式的變量每次都會獲得Z

123Z
123 Z
123        Z
123
               Z

(對於最后的輸入,最后兩行被一起讀取。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM