【发布时间】:2014-05-15 18:35:20
【问题描述】:
我的 for 循环有问题。它通过两个数组并比较帐号和 PIN。 if 语句检查帐号和 PIN 的组合是否正确。由于我从原来的(user_Account == accounts[i] && user_Pin != pins[i]) 更改了这部分(user_Account != accounts[i] || user_Pin != pins[i]),因此发生了这种情况。在此之前它运行良好,但我担心如果有人输入错误的帐户,那么程序可能会崩溃,所以我做了这个改变。这是代码的一部分,如果需要,我可以发布更多。请记住一件事,我正在开始上课,所以没有高级更改或建议,我需要当前水平的东西。感谢您的帮助。
for (int i = 0; i < 6; i++)
{
if(user_Account == accounts[i] && user_Pin == pins[i])
{
cout << "You entered correct combination of account and pin number." << endl;
}
else if(user_Account != accounts[i] || user_Pin != pins[i])
{
cout << "You entered wrong account and/or wrong pin number. Please start over." << endl;
return 0;
}
}
【问题讨论】:
-
如果是
if (x) else if (!x),就去掉第二个if。尝试修复逻辑反转以及完全没有必要时,您将省去头疼的问题。 -
就像 PIN 代表个人识别号码一样,PIN 号码将是个人识别号码...... :)
标签: c++ loops if-statement for-loop