Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如果陈述是错误的,你如何循环?_C - Fatal编程技术网

C 如果陈述是错误的,你如何循环?

C 如果陈述是错误的,你如何循环?,c,C,我刚刚开始用C语言编写代码,我想我会尝试一些我认为简单的东西。我让它工作,现在我希望它循环,如果它是假的,所以我可以再次输入一个数字。帮忙 #include <stdio.h> int main() { int a; printf("Enter The Passcode: "); scanf("%d", &a); if (a != 625){ printf("Correct Passcode"); } else

我刚刚开始用C语言编写代码,我想我会尝试一些我认为简单的东西。我让它工作,现在我希望它循环,如果它是假的,所以我可以再次输入一个数字。帮忙

#include <stdio.h>

int main()
{
    int a;
    printf("Enter The Passcode: ");
    scanf("%d", &a);
    if (a != 625){
        printf("Correct Passcode");
    }
   else if (a == 625){
        printf("Incorrect Passcode");
}
    return(0);
}
#包括
int main()
{
INTA;
printf(“输入密码:”);
scanf(“%d”和“&a”);
如果(a!=625){
printf(“正确的密码”);
}
如果(a==625),则为else{
printf(“不正确的密码”);
}
返回(0);
}

为了让您指向正确的方向,您可以在这里使用
循环。有一个布尔表达式声称某件事不是真的与有一个布尔表达式声称它是真的没有本质上的区别<代码>1!=例如,2是完全正确的(因为1实际上并不等于2)。这听起来有点奇怪,但想想为什么这句话是真的,我想这会澄清问题

一个要点。我知道注释中提到了这一点,但在本例中,
else if(a==625)
是多余的,因为
a
在这里可能只有625。另外,我假设您颠倒了
if
语句,因为您写这句话的方式是正确的密码(仔细查看
if
语句以了解为什么会这样)

话虽如此,以下是一个可能对您有所帮助的示例:

int a = // Read integer from console;

// This will happen if, and only if, a is something other than 625
// This'll keep prompting them until they enter 625
while (a != 625) {
   printf("Incorrect password. Please enter the correct password.");
   a = // Read integer from console
}

// If we got past the loop, we know that they must have entered a correct password
printf("Correct password");

希望这能有所帮助。

为此,您必须使用loop,如

#include <stdio.h>

int main()
{
    int a=0; //some random
    printf("Enter The Passcode: ");


while(a!=625){
     scanf("%d", &a);
     if (a != 625){
        printf("Correct Passcode");
        break; // you found correct
     }
     else if (a == 625){
        printf("Incorrect Passcode");
      }

   }
    return(0);
}
#包括
int main()
{
int a=0;//一些随机变量
printf(“输入密码:”);
而(a!=625){
scanf(“%d”和“&a”);
如果(a!=625){
printf(“正确的密码”);
break;//您找到了正确的
}
如果(a==625),则为else{
printf(“不正确的密码”);
}
}
返回(0);
}

从编写循环开始。您是否尝试在此处搜索用户输入的c循环?
如果(a==625)
是多余的,因为当涉及到该子句时,它必须等于625,这就是“else”的含义。不要使用括号作为回报,这不是函数hi,欢迎使用堆栈溢出。很抱歉,这个问题可能会因为太琐碎而结束。你应该像读一本关于学习C语言的好书。老实说,如果您只是在学习如何编程,C可能不是一个好的选择。这很复杂,最好像first一样用高级语言学习基础知识。谢谢大家的回答。我将搜索用户输入的c循环。好吧,我知道你们要关门了。谢谢你的建议。@NoiDea我希望我的解决方案对你有用