代码之家  ›  专栏  ›  技术社区  ›  Shreyas Pednekar

打印53到96之间7的倍数

c#
  •  0
  • Shreyas Pednekar  · 技术社区  · 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();
    

    输出:

    output

    输出应为:56、63、70、77、84、91

    4 回复  |  直到 7 年前
        1
  •  4
  •   Derviş Kayımbaşıoğlu    7 年前

    非常基本的方法

    int tbl=53;
    while  (tbl < 96)
    {
       if (tbl % 7 == 0)
          Console.WriteLine(tbl);
    
       tbl++;
    }
    
        2
  •  3
  •   Uwe Keim    7 年前

    这是最好也是最快的方法,当你碰到一个可以被7整除的数字时,你会继续增加7,而不是1

    int tbl = 53;
    
    while  (tbl < 96)
    {
       if (tbl % 7 == 0){
          Console.WriteLine(tbl);
          tbl+=7;
          continue;
       }
       tbl++;
    }
    
        3
  •  0
  •   Dmitrii Bychenko    7 年前

    每一个 7 第17项, 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();
    
        4
  •  -1
  •   kunquan    7 年前

    这是必须的 && ||