为什么在此开关案例中不重复我的时循环?
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
int if_again = 1;
int c = 1;
switch (a)
{
case 1:
while (if_again == 1)
{
scanf("%d", c);
if (c == 1)
{
if_again == 1;
}
else
{
if_again == 0;
}
}
}
return 0;
}
仅给出 1 两次后就会终止,但它应该继续重复,直到输入 0。我的代码有什么问题吗?
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
int if_again = 1;
int c = 1;
switch (a)
{
case 1:
while (if_again == 1)
{
scanf("%d", c);
if (c == 1)
{
if_again == 1;
}
else
{
if_again == 0;
}
}
}
return 0;
}
It is getting terminated after giving 1 two times only, but it should keep on getting repeated until 0 is entered. What is wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SCANF需要您提供的变量的基本地址。因此
scanf(“%d,c);
需要为scanf(%d,&amp; c);
以下代码也是错误的:
编译器认为您是将“ if_again”与1进行比较。我认为您正在尝试将变量设置为1:
是正确的。
else
语句 如果您想看到它a
是1,则只需scanf needs the base address of a variable you give it. So
scanf("%d, c);
needs to bescanf(%d, &c);
The following code is also wrong:
The compiler thinks that you are comparing "if_again" with 1. I think you are trying to set the variable to 1? In that case do this:
The same is true for the
else
statement.Also, why is there a switch statement? If you want to see it
a
is 1, then just to anif