以下代码已尽可能简化,假设执行以下操作:
1)用户填充5个整数的列表。
2)用户输入'数字'。
3a)如果'number'与5个整数中的一个相同 - 将显示该数字的两倍,然后重复2。
3b)IF'number'与5个整数之一 - 结束程序不同。
但是在运行时我输入一个在列表中的数字,并得到输出响应,说明它不在列表中。也有了这个else语句被激活,人们会期望while循环停止为a = 0,但是它会继续循环。
这里是我认为关于未在列表中显示的数字的错误:
if (number == list[x])
我可能在括号内使用了错误的陈述。
但是我不知道为什么它也会继续循环,也许这两个错误都是因为同样的事情。任何有关如上所述运行程序的帮助将不胜感激。
以下是完整代码:
#include <iostream>
using namespace std;
int main()
{
int list[5];
int a, x, number;
a = 1;
cout << "Fill up the list" << endl;
for (x = 0; x <= 4; x++){
cin >> list[x];
}
while (a = 1) {
cout << "enter one of the numbers you put on the list" << endl;
cout << "we will double it " << endl;
cin >> number ;
if (number == list[x])
{
cout << "double that number is " << number * 2 << endl;
}
else
{
cout << "Thats not on the list" << endl;
a = 0;
}
}
return 0;
}
答案 0 :(得分:0)
我已经改变了
if (number == list [x])
到
if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))
并更改了
while (a = 1)
到
while (a == 1)
然而,如果有人知道替代
if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))
我愿意接受建议。
完整的代码现在是:
#include <iostream>
using namespace std;
int main()
{
int list[5];
int a, x, number;
a = 1;
cout << "Fill up list bitch" << endl;
for (x = 0; x <= 4; x++){
cin >> list[x];
}
while (a == 1) {
cout << "enter one of the numbers you put on the list" << endl;
cout << "we will double it " << endl;
cin >> number ;
if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))
{
cout << "double that number is " << number * 2 << endl;
}
else
{
cout << "Thats not on the list you scumbag" << endl;
a = 0;
}
}
return 0;