【问题标题】:Simple "Random Number Guessing Game" / IF ELSE in C not working简单的“随机数猜谜游戏”/如果 C 中的 ELSE 不起作用
【发布时间】:2013-10-14 21:23:01
【问题描述】:

我刚刚开始我的编程入门课程(所以请多多包涵),但我有点卡在第一个作业上。

我应该编写一个数字猜谜游戏,将 1 到 10 之间的随机数存储到一个变量中,提示用户输入一个数字,并通知用户是否猜到了相同的数字。

我已经搞砸了一段时间了,代码与我开始时的代码相比发生了很大变化。目前,无论我猜如何,该程序都在说“恭喜,你是赢家”......

如果有人能指出我做错的方向,那就太好了。

自最初发布问题以来,代码已被编辑

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>

int main()
{

    //Declare Variables 
    int RandomNum;
    int UserGuess;

    //Initialize Variables
    RandomNum=0;
    srand ( (unsigned)time ( NULL ) );
    char UserInput = 'a';
    int a = UserInput;

    //Generate RandomNum
    RandomNum = (rand() % 10)+1;

    //Prompt User for UserInput
    printf("Guess the random number!\n");
    printf("Enter your guess now!\n");
    scanf("%d", &UserInput);

    //Determine Outcome
    if (UserInput == RandomNum) 
        printf("You're a WINNER!\n");
    else
        printf("Incorrect! The number was %d\n", RandomNum);

    //Stay Open
    system("PAUSE");
}

【问题讨论】:

  • 这个UserGuess=atoi("UserInput"); 很糟糕,需要删除。这个scanf("%d", &amp;UserInput); 试图将int 读入char,这也不是好消息。
  • 您仍在尝试将int 填充到char 中。将char UserInput = 'a'; int a = UserInput; 更改为int Userinput = 0;。你不会使用a 来做任何事情,所以别管它了。删除UserGuess,因为你不使用它。

标签: c if-statement random


【解决方案1】:

改变这一行 -

if (UserGuess = RandomNum)

到这里——

if (UserInput == RandomNum)

第一个分配存储在RandomNum中的用户输入到UserGuess,然后隐式转换为真或假,然后if条件的真值为由编译器检查。我假设您正在输入非零值作为程序输入。如果是这种情况,那么 C 将认为它是真的。事实上,任何非零值(无论是正数、负数还是小数)都被 C 认为是真的。

第二个表达式检查两个变量是否相等,而不是将一个分配给另一个。因此,您将获得所需的行为。

【讨论】:

  • 我不敢相信我错过了。 D:唉,我还有一个问题。我将输入“2”,但仍然收到消息“不正确,数字为 2”
  • @AllisonMcKillop:这是因为您将用户输入存储到UserInput 变量中,而在if 中,您正在检查UserGuess 变量。我会更新我的答案。
  • 我进行了您推荐的更改,但每次收到“else”或不正确的消息时我仍然遇到同样的问题,即使它说随机数是 2 而我输入了 2。
  • @AllisonMcKillop:那是因为您将2 作为字符输入,其ASCII 值存储在等于50 的UserInput 变量中。将%c 更改为@987654332 @在scanf,你很好。
  • 好的,我已经更新了我原来帖子中的代码,以反映(我认为)你在说什么。现在我每次都得到“不正确,数字为 0”。
【解决方案2】:

您将=== 混淆了

if (UserGuess = RandomNum) 不会给出你想要检查猜测是否等于随机未生成的布尔结果..

使用

if (UserGuess == RandomNum) 

【讨论】:

  • 谢谢。我解决了这个问题,但仍然有问题。即使我猜对了数字,我也会得到“else”或不正确的响应。
【解决方案3】:

您的if 不正确。 == 是相等,= 是赋值。

【讨论】:

  • 我不敢相信我错过了。 D:唉,我还有一个问题。我将输入“2”,但仍然收到消息“不正确,数字为 2”
【解决方案4】:

更改UserInput的类型,去掉UserGuess和对atoi()的虚假调用,就可以了:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {

//Declare Variables
    int RandomNum;
    int UserInput;      //  Changed to int

//Initialize Variables
    RandomNum = 0;
    UserInput = 0;      // Changed initialization
    srand((unsigned) time(NULL));

//Generate RandomNum
    RandomNum = (rand() % 10) + 1;

//Prompt User for UserInput
    printf("Guess the random number!\n");
    printf("Enter your guess now!\n");
    scanf("%d", &UserInput);

//Determine Outcome
    if (UserInput == RandomNum)
        printf("You're a WINNER!\n");

    else
        printf("Incorrect! The number was %d\n", RandomNum);

//Stay Open
    system("PAUSE");
}

【讨论】:

  • 完美。我发誓我之前试过这个,我一定是做错了什么,哈哈。谢谢。
【解决方案5】:

你确定atoi() 函数接受一个整数参数吗? 因为atoi()函数用于将字符串转换为整数。

阅读this article

【讨论】:

  • 我认为你是对的。我使用了以下“char c = 'a'; // 缩小 C int a = c;”作为向导。一旦我更好地测试它,我将更新我当前的代码
  • 查看更新代码;它有效,但是当我取出“int UserGuess”时,每个随机数都是0。为什么会发生这种情况,因为它是一个未使用的变量?此外,“a”是未使用的变量。我收到有关“int 格式、不同类型、arg”和“控制到达非 void 函数的末尾”的警告我不知道这些是什么意思
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2018-09-11
相关资源
最近更新 更多