hc学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › fsrm › 日志

【方法】在类中编写一个方法,判断数字是否是素数

2022-07-11 15:27:44 查看(4) 回复(0)

要求:

传入参数:10,返回:false

传入参数:11,返回:true

public static void main(String[] args) {
        demo count = new demo();
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数字");
        int n = scan.nextInt();
        if (count.ss(n)){
            System.out.println("素数");
        }else {
            System.out.println("不是素数");
        }
}
public static  Boolean ss(int x ){
        boolean flag = true;
        for (int i = 2;i<x;i++){
            if (x%i==0){
                flag = false;
                break;
            }
        }
        return  flag;
    }

评论 (0 个评论)

facelist

全部作者的其他最新日志



站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

f1208.com 备案号:京ICP备14030918号-1

返回顶部