如何让这个程序每行打印10个项目,用1个空格分隔?

时间:2014-10-07 22:06:09

标签: java loops

到目前为止,这是我的代码

public class DivisibleBy5and6
{
   public static void main (String []args)
   {
      for (int i = 100; i <= 200; i++)
      {
         boolean num = (i % 5 == 0 || i % 6 == 0) && !(i % 5 == 0 && i % 6 == 0);

         if (num == true)
            System.out.println(i + " is divisible");
      }
   }
}

如前所述,我怎样才能让输出打印出每行以空格分隔的10个项目?

1 个答案:

答案 0 :(得分:1)

怎么样:

int count = 0;
    for (int i = 100; i <= 200; i++) {
        boolean num = (i % 5 == 0 || i % 6 == 0) && !(i % 5 == 0 && i % 6 == 0);

        if (num == true) {
            count++;
            System.out.print(i + " is divisible ");
            if(count >= 10) {
                System.out.println();
                count -= 10;
            }
        }
    }