static void Main(string[] args)
{
int[] num = new int[] { 3, 34, 43, 2, 11, 19, 30, 55, 20 };
bool hasit = false;
for (int i = 0; i < num.Length; ++i)
{
if (num[i] % 7 == 0)
{
hasit = true;
break;
}
}
if (hasit)
{
Console.Write("有七的倍数");
}
else
{
Console.Write("没有七的倍数");
}
}
}
如图代码,把for语句换成foreach, if (num[i] % 7 == 0)这一句就会报错,请问为什么呀
换 foreach,要把break调到if外面,即:
foreach(int i in num)
{
if (num[i] % 7 == 0)
{
hasit = true;
}
break;
}
foreach 遍历所有元素,不能用索引了就,直接用定义的变量
foreach (int i in num) { if (i % 7 == 0) { } }