2017-01-25 34 views
-4

我刚刚开始编码在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); 
} 
+0

开始。您是否尝试在此处搜索用于用户输入的* c循环*? –

+1

'if(a == 625)'是多余的,因为当涉及到该子句时,它必须等于625,这就是“其他”的含义。并且不要使用括号作为回报,它不是函数 –

+0

嗨,欢迎来到Stack Overflow。对不起,但这个问题可能会因为太小而关闭。你应该看看[while循环](https://www.tutorialspoint.com/cprogramming/c_while_loop.htm)和一本学习C的好书,例如[Learn C The Hard Way](https://learncodethehardway.org/c /)。老实说,如果你只是在学习如何编程,C可能不是一个好的选择。这很复杂,最好先用高级语言学习基础知识,比如[Ruby](http://tryruby.org/levels/1/challenges/0)。 – Schwern

回答

0

为了让您指出正确的方向,您需要在这里使用while循环。这不是本质上的区别有一个布尔表达式声称的东西的的情况以外有一个声称这是真的。 1 != 2,例如,是完全正确的(因为1实际上等于2)。这听起来有点奇怪,但想想为什么这个陈述是真实的,我想这会澄清问题。

一个快速点。我知道,这是在评论中提到,但在这种情况下else if (a == 625)是多余的,因为a不可能是什么 625在这里。另外,我认为你扭转你的if语句,因为这样你写了这个从字面上任意数量的其他比625是正确的密码(你可以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"); 

希望这有助于。

0

你必须写一个循环中使用循环用于此目的的类似

#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); 
}