题解 | #牛牛的素数和#

牛牛的素数和

https://www.nowcoder.com/practice/d748d79f68ab443482c5547d93824f50

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int l=in.nextInt();
        int r=in.nextInt();
        int count=0;
        for(int i=l;i<=r;i++){
            if(isNum(i)){
                count+=i;
            }
        }
        System.out.print(count);
    }
    public static boolean isNum(int num){
        if(num==2){
            return true;
        }else if(num<2){
            return false;
        }else{
            for(int i=2;i<num;i++){
                if(num%i==0){
                    return false;
                }
            }
        }
        return true;
    }
}

全部评论

相关推荐

07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务