2013-04-20 31 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