【问题标题】:Generating a dice game - C Programming生成骰子游戏 - C 编程
【发布时间】:2015-08-29 21:11:50
【问题描述】:

我正在关注 youtube 上的教程,并且正在做一个骰子生成器。 它基本上打印出3个骰子结果并总结出骰子结果。 之后,用户将查看总和,并根据总和来猜测下一次滚动是更高、更低还是相同。

下面是我的代码,假设当我输入“是”时,它应该在 if 语句中执行代码。但是,它直接进入了 else 语句。谁能告诉我怎么了?

int answer;
int guess;
int diceRoll4 = 0;
printf("Would you like to guess your next dice? Y/N \n");
scanf(" %c", &answer);

if (answer == 'yes' ){

    printf("What is your guess?\n");
    printf("please key in your number \n");
    scanf(" %d", &guess);
    if (guess > diceRoll4 ){
        printf(" You got it wrong, too high!");
    }
    else if (guess < diceRoll4){
            printf(" You got it wrong, too low!");
    }
    else {
        printf("You got it right");
    }

}
else{
    printf("Thanks for playing");
}

【问题讨论】:

  • 除其他外,scanf(...%c.. 读取单个字符..
  • 发布的代码既不能编译也不是完整的程序。请在发布存在运行时问题的代码时。发布干净编译并显示问题的代码。

标签: c dice


【解决方案1】:

这一行:

如果(答案 == '是'){

有几个问题。

1) the definition of 'answer' is 'int' but the scanf is inputting a single character

2) answer could be compared with 'y' or 'n' but not to a array of char.

3) since the scanf only input a single char 
   and you/the user input 'yes', 
   only the first character was consumed, 
   so the 'es' are still in the input buffer

4) note the the single character could be anything, except white space.
   the leading space in the format string would consume any white space.
   so the user could input say 'y' or 'Y'  
   these are different characters
   however, using the toupper() macro from ctypes.h
   would mean only a 'Y' would need to be compared

5) if you decide to read a string, 
   then 'answer' needs to be a character array, 
   say:  char answer[10];
   and the scanf needs to have a max length modifier 
   on the associated "%s" input/conversion parameter
   so as to avoid the user overflowing the input buffer
   and the comparison would be via the strcmp() function

6) always check the returned value (not the parameter value) 
   from scanf to assure the operation was successful

7) diceRoll4 and guess can never be a negative number
   so the variable definitions should be unsigned
   and the associated scanf() for guess should use 
   something like "%u"

8) on the printf() format strings, always end them with '\n' 
   so the sting will be immediately displayed to the user, 
   otherwise, they will only be displayed 
   when a input statement is executed or the program exits

【讨论】:

    【解决方案2】:

    要测试相等性,您必须使用strcmp。如果返回值为0,则表示它们相等。

    if (strcmp(answer, "yes") == 0) {
        // ...
    } else {
        // ...
    }
    

    注意事项:

    1. 仅使用 answer == 'yes' 它测试指针的相等性而不是值。这就是为什么只输入else的原因。

    2. 因为answerint 你必须改成数组

      char answer[15]
      
    3. 正如@Sathya 提到的,您正在阅读一个字符 %c 来读取您必须使用的字符串 %s

      scanf("%s", answer);
      
    4. 而不是'yes'多字符字符常量更改为"yes",这是一个char的数组\0位于最后,more informations here

    【讨论】:

      【解决方案3】:

      'yes' 是一个多字节字符,其行为由实现定义。

      您可能想要阅读和比较单个char

      if (answer == 'y' ){
      

      或读取整个字符串并进行比较:

      char answer[128];
      scanf("%s", answer);
      if ( strcmp(answer,"yes") == 0 ){
      ...
      }
      

      请注意,我更改了answertype,并使用%s 读取了一个字符串

      【讨论】:

        【解决方案4】:

        如果您不想读取字符串,而只想读取单个char,用户可以回答YN,则应将int answer; 更改为char answer;。然后你可以继续使用你原来的scanf()-call。你仍然需要改变

        if (answer == 'yes')
        

        if (answer == 'Y')
        

        如果您希望用户输入yY,您可以将toupper()ctype.h 更改为if 条件为if (toupper(answer) == 'Y')

        【讨论】:

          【解决方案5】:

          首先,answer 应该是 chars 的数组,以便保存字符串。改变

          int answer;
          

          char answer[10]; //Or any other reasonable size
          

          其次,既然要扫描的是字符串而不是字符,那就改

          scanf(" %c", &answer);
          

          scanf("%9s", answer);
          

          9 将扫描最多 9 个字符(末尾的 NUL 终止符 +1),从而防止buffer overflows
          我已经删除了&amp;,因为%s 期望char*&amp;answer 将给出char(*)[10]。数组的名称被转换为指向其第一个元素 char* 的指针,这正是 %s 所期望的。上面的scanf 因此等价于

          scanf("%9s", &answer[0]);
          

          第三,使用== 比较两个字符串是比较指针而不是其中的实际内容。请改用string.h 中的strcmp。当它的两个参数包含相同的内容时,它返回 0。改变

          if (answer == 'yes' ){
          

          if (strcmp(answer, "yes") == 0){
          

          双引号用于表示以 NUL 结尾的字符串 (char*),这正是 strcmp 所期望的,而在您的代码中,单引号是一个多字符文字,其值由实现定义.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-02-29
            • 2015-08-25
            • 1970-01-01
            • 2013-10-14
            • 2012-11-27
            • 1970-01-01
            • 2015-05-29
            相关资源
            最近更新 更多