生成骰子游戏 - C 编程

Generating a dice game - C Programming

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

下面是我的代码,假设,当我输入 'yes' 时,它应该在 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");
}

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

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

备注:

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

  2. 的原因
  3. 因为answerint所以要换成数组

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

    scanf("%s", answer);
    
  5. 而不是'yes',它是多字符字符常量,改为"yes",它是一个数组char[=24=] 在末尾,more informations here.

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

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

if (answer == 'y' ){

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

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

请注意,我更改了 answertype 并使用 %s 读取 string

首先,answer 应该是一个 char 的数组,以便保存一个字符串。变化

int answer;

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

其次,既然你想扫描一个字符串而不是一个字符,改变

scanf(" %c", &answer);

scanf("%9s", answer);

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

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

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

if (answer == 'yes' ){

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

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

如果您不想读入一个字符串,而只想读入一个用户可以回答 YNchar,您应该更改 int answer;char answer;。然后您可以继续使用原来的 scanf()-call。您仍然需要更改

if (answer == 'yes')

if (answer == 'Y')

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

这一行:

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

有几个问题。

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