如何每行打印n个数字

Dav*_*vid -1 java

您好我有一个问题,我必须打印出可被17整除的数字,但我想打印5个数字,然后跳到下一行.这是我的代码,不知道为什么它不起作用....

public class seventeen {
   public static void main(String[] args) {
      int num = 17;
      System.out.print("The Numbers Divisible By 17 are: ");
      int enter = 0;
      for (int x = 1; x <= 10000; x++) {
         if (x % num == 0) {
            System.out.print(x + " ");
         }
         enter++;
         if (enter == 5) {
            enter = 0;
            System.out.println();
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

Lui*_*oza 6

您应该只在达到数字验证时递增计数器.这意味着,移动enter++;内部验证:

if (x % num == 0) {
    System.out.print(x + " ");
    enter++;
}
Run Code Online (Sandbox Code Playgroud)