开发者

Add numbers from 100 to 1000 that are divisible by five AND six

开发者 https://www.devze.com 2023-04-11 13:36 出处:网络
My program is correct except for that in the end, the program must output two \\n\\n. Just like that. Only on the last line, however. At every ten sets, it should only output one \\n. THis is what the

My program is correct except for that in the end, the program must output two \n\n. Just like that. Only on the last line, however. At every ten sets, it should only output one \n. THis is what the program should look like:

1   2   0       1   5   0       1   8   0       2   1   0
2   4   0       2   7   0       3   0   0       3   3   0
3   6   0       3   9   0  \n   4   2   0       4   5   0
4   8   0       5   1   0       5   4   0       5   7   0
6   0   0       6   3   0       6   6   0       6   9   0  \n
7   2   0       7   5   0       7   8   0       8   1   0
8   4   0       8   7   0       9   0   0       9   3   0
9   6   0       9开发者_如何学运维   9   0  \n  \n

This is my code.

import java.util.Scanner;

public class Exercise4_10 {
    public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          int count = 1;
          for (int i = 100; i <= 1000; i++) {
              if (i%5==0 && i%6==0)
              System.out.print((count++ % 10 != 0) ? i + " ": i + "\n" );
          }
    }
}


add this before the closing brace for main:

System.out.println("");
}


import java.util.Scanner;

public class Exercise4_10 {

   public static void main(String[] args) {
          Scanner input = new Scanner(System.in);

        for (int i = 100; i <= 1000; i++) {

            // You can avoid 5 & 6 by using 30 ( 5*6) - optional
            if (i%30==0)

                //You can skip using another variable count & use i for the same - optional
                System.out.print(((i+1) % 10 != 0) ? i + " ": i + "\n" );

        }

        //Will print \n only in the end
        System.out.print("\n" );
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消