我想打印53-96之间7的倍数。
代码:
int tbl = 0;
while(!(tbl > 53) || !(tbl < 96))
{
tbl = tbl + 7;
while(tbl > 53 && tbl < 96)
{
Console.WriteLine(tbl);
tbl = tbl + 7;
}
}
Console.ReadLine();输出:

产出应该是: 56,63,70,77,84,91
发布于 2018-12-13 06:02:42
非常基本的方法
int tbl=53;
while (tbl < 96)
{
if (tbl % 7 == 0)
Console.WriteLine(tbl);
tbl++;
}发布于 2018-12-13 06:06:57
这是最好也是最快的方法,当你碰到一个可以被7整除的数字时,你会继续增加7而不是1。
int tbl = 53;
while (tbl < 96)
{
if (tbl % 7 == 0){
Console.WriteLine(tbl);
tbl+=7;
continue;
}
tbl++;
}发布于 2018-12-13 06:37:35
由于我们希望打印出每个7第四项,所以for循环似乎是最简单的选择:
int start = 53;
int stop = 96;
for (int tbl = (start / 7 + (start % 7 == 0 ? 0 : 1)) * 7; tbl < stop; tbl += 7)
Console.WriteLine(tbl);
Console.ReadLine();如果53值是固定的,我们可以预先计算开始值:(53 / 7 + (53 % 7 == 0 ? 0 : 1)) * 7 == (7 + 1) * 7 == 56
for (int tbl = 56; tbl < 96; tbl += 7)
Console.WriteLine(tbl);
Console.ReadLine();https://stackoverflow.com/questions/53755776
复制相似问题