为了让您指出正确的方向,您需要在这里使用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");
希望这有助于。
开始。您是否尝试在此处搜索用于用户输入的* c循环*? –
'if(a == 625)'是多余的,因为当涉及到该子句时,它必须等于625,这就是“其他”的含义。并且不要使用括号作为回报,它不是函数 –
嗨,欢迎来到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