打印 53 到 96 之间 7 的倍数

我想打印 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();

输出:

http://img3.mukewang.com/62a58cf90001389109740546.jpg

输出应该是: 56, 63, 70, 77, 84, 91 它应该在 91 处停止,但不是在 91 处停止



慕尼黑5688855
浏览 166回答 3
3回答

四季花海

非常基本的方法int tbl=53;while&nbsp; (tbl < 96){&nbsp; &nbsp;if (tbl % 7 == 0)&nbsp; &nbsp; &nbsp; Console.WriteLine(tbl);&nbsp; &nbsp;tbl++;}

弑天下

这是做到这一点的最好和最快的方法,当你碰到一个能被 7 整除的数字时,你继续增加 7 而不是 1int tbl = 53;while&nbsp; (tbl < 96){&nbsp; &nbsp;if (tbl % 7 == 0){&nbsp; &nbsp; &nbsp; Console.WriteLine(tbl);&nbsp; &nbsp; &nbsp; tbl+=7;&nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp;}&nbsp; &nbsp;tbl++;}

幕布斯7119047

由于我们想打印出每 7一项,for循环似乎是最简单的选择:int start = 53;int stop = 96;for (int tbl = (start / 7 + (start % 7 == 0 ? 0 : 1)) * 7; tbl < stop; tbl += 7)&nbsp; &nbsp;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)&nbsp;&nbsp; Console.WriteLine(tbl);&nbsp;Console.ReadLine();
打开App,查看更多内容
随时随地看视频慕课网APP