weixin_45687020 2021-06-24 15:20 采纳率: 100%
浏览 25
已采纳

质数输出,为什么我用continue方法有问题

public class PrimeNumberTest {


    public static void main(String[] args) {


        int count=0;
        
        label:for(int i=2;i<=100;i++) {
            
            for(int j = 2;j<Math.sqrt(i);j++) {


                if(i%j == 0) {
                    continue label;
                }
            }
            count++;
            System.out.println(i);
            
        }
        System.out.println("质数个数:"+count);
        
    }

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-06-24 15:27
    关注

    for(int j = 2;j<Math.sqrt(i);j++) {

    改成

    for(int j = 2;j<=Math.sqrt(i);j++) {

    你没有成功过滤掉的正好是平方数。所以这个条件必须是 j<=Math.sqrt(i),等于也是要考虑的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音