【问题标题】:Guessing Game - Can't figure out why it produces a large negative integer [closed]猜谜游戏 - 无法弄清楚为什么它会产生一个大的负整数 [关闭]
【发布时间】:2018-08-06 08:49:46
【问题描述】:

干杯,伙计们。我生成的这段代码有问题。它的功能一切正常,但是当我猜对时它打印数字时,它会打印一个我不明白的数字,例如 -3529583。有什么启发?

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

int main()
{
    int y, iRandomNum1; // Declare the three variables
    y = 0;
    iRandomNum1 = 0;
    srand(time(NULL)); // Randomize function
    iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value

    while (iRandomNum1 != y) {
        printf("Guess a number from 1 to 10\n");
        scanf("%d", &y);
    }
    printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
    return 0;
}

【问题讨论】:

  • 您正在打印y 的地址,您应该正在打印该值,只需在打印功能中删除&amp;。另请注意,也许您应该从y=-1 开始,就像现在一样,如果您的随机值也是 0,您将永远猜不到。
  • &amp;y); in printf?
  • 哦,对了!非常感谢你们
  • 您可能还想将 1 添加到 iRandomNum1,以将其置于您似乎期望的范围 [1-10] 中。没有它,它在 [0-9] 范围内。

标签: c


【解决方案1】:

您将在printf 中显示变量&amp;y 的地址而不是变量本身,只需删除&amp; 符号就可以了

https://stackoverflow.com/users/2173917/sourav-ghosh 在评论中已经给出了答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2020-11-23
    • 2020-08-31
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多