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