两行数字之间为什么会空一大片?

    public static void printPrime(){
        int count = 0;
        for(int i = 1; i <= 100; i++){
            int j = 2;
            while( j < i){
                if(i%j == 0){
                    break;
                }
                j++;
            }
            if(j==i){
                System.out.print(i +" ");
                count++;
            } 
            if(count % 5 == 0){
                System.out.println();
            }
        }
        System.out.println("个数为: "+count);
    }

显示结果是:
图片描述

第3行数字和第4行数字之间怎么会空一大片?最后一行数字和文字之间怎么又空一大片?

阅读 1.9k
2 个回答

按照你的写法,当i为下列数字的时候,会输出回车;

// 输出回车时候的i
[1, 11, 12, 29, 30, 47, 48, 49, 50, 51, 52, 71, 72, 97, 98, 99, 100]
// 此时count值为
[0, 5, 5, 10, 10, 15, 15, 15, 15, 15, 15, 20, 20, 25, 25, 25, 25]

在count为15,25的时候,会输出很多行。

这种问题一般先给所有的数字求出来,在统一打印,就好处理的多。

49,51去哪了,太复杂了,可以改为

for(int i = 1; i <= 100; i++){
            if(i%2 == 0){
                continue;
            }
            
             System.out.print(i +" ");
             count++;
            
            if(count % 5 == 0){
                System.out.println();
            }
        }
        System.out.println("个数为: "+count);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题