using System;
using System.Collections.Generic;
using System.Text;
namespace 循环中断计算
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int i = 1;
while (i <= 100)
{
if (i % 7 == 0) //利用"取余"来验证当前i是否能被7整除,如果能整除余数为0;
{
i++;
continue; //continue之前要自增,contiue不会执行后面代码,直接跳到while中
}
sum = sum + i;
i++;
}
Console.WriteLine("{0}", sum);
Console.ReadKey();
}
}
}
本文提供了一个使用C#编写的示例程序,通过while循环计算1到100之间所有不能被7整除的数的累加和,并展示了如何使用continue语句跳过特定条件下的迭代。
2万+

被折叠的 条评论
为什么被折叠?



