我试图查看数字是否可以被数组中的任何给定数字整除。 我见过很多答案,其中number1%number2 == 0的答案,但我还没有找到任何数字的数组,我宁愿不写出数组中的每个数字我自己(这也会工作硬)。是否可以被数组中的任何数字整除
所以这里是我想弄明白。正如你所看到的,当前工作的唯一方式是如果我给出的最后一个数字是真的,否则else语句会启动并将number1_result赋值为false。
int square_numbers[10] = { 4, 9, 25, 36, 49, 64, 81, 100, 121, 144 };
int number1;
bool number1_result;
cin >> number1;
for (int i = 0; i < 10; i++) {
if(number1 % square_numbers[i] == 0)
number1_result = true;
else
number1_result = false;
}
我该如何确定如果循环曾经评估为true,那么这是真的。如果至少有一次不是这样,那就是错误的。
让我知道你是否需要更多信息,谢谢!
'number1_result == true/false'是一个条件,将'=='更改为'=' –
找到除数后立即断开循环。没有必要继续检查其他人。 – NathanOliver
[打开警告。](http://coliru.stacked-crooked.com/a/e9cadc9857b846e5) – chris