正如您在那看到的那样,问题是用户输入他的代码后循环不会继续,我想知道为什么会这样,以及您是否对我有更好的用途。我是新手,对C语言的帮助非常感谢!!!!!!
#include <stdlib.h>
#include <stdio.h>
int main()
{
int randomNumber = 11;
int usersGuess;
int i;
do {
printf("You need to guess a number between 0 and 20! Good Luck! \n");
for (i = 5; i > 0; i--) {
printf("You have got %d amount of tries, Guess The random number: ", i);
scanf_s("%d", usersGuess);
if (usersGuess == randomNumber) {
printf("You won");
break;
} else if (usersGuess > randomNumber) {
printf("That is wrong, random number is less than that");
} else if (usersGuess < randomNumber) {
printf("that is wrong, the random number is higher than that");
} else if (usersGuess > 20) {
printf("please guess again cause the random number is between 0 and 20");
}
}
} while(i > 0);
return 0;
}
答案 0 :(得分:2)
您的代码具有未定义的行为,这意味着它存在错误,可能会发生任何事情。问题是您要向scanf_s
传递一个整数,该整数需要一个指针。这样做:
scanf_s("%d", &usersGuess);
原因是您希望函数将其写入变量usersGuess
。在C语言中,所有参数都按值传递,因此,如果要使用输出参数,则必须使其成为指针。