我是 C 编程新手,我试图编写一个简单的程序,要求用户重新排列屏幕上显示的数字,但我遇到了问题,我会在屏幕上打印出不同的数字我分配给变量的值。为什么会得到不同的数字?这是我遇到的问题和我的代码的屏幕截图 the image :
#include <stdio.h>
main()
{
int numOne, numTwo, numThree, ansOne, ansTwo, ansThree;
char name[20];
numOne=34521;
printf("\nWelcome to scrambled numbers Game");
printf("\n Please input your name to get started: ");
scanf("%s", name);
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
scanf("%d", &ansOne);
if(ansOne==12345)
{
printf("Congratulations %s you have won the first round", name);
}
else
{
printf("sorry %s you failed the first round", name);
}
}
最佳答案
发生这种情况是因为您正在打印变量 numOne 的地址而不是 numOne 本身。
尝试从 printf 中的 numOne 中删除 & 运算符。
替换这个:
printf("\n按升序重新排列这些数字 %d :", &numOne);
通过这个:
printf("\n按升序重新排列这些数字 %d :", numOne);
关于c - 为什么声明变量后我得到不同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117310/