JAVA基础训练题(含答案)

1、判断一个数是否是水仙花数

public class shuixianhua {
    public static void main(String[] args) {
        int sx = getSxh(153);
        boolean s=true;
    }

    public static int getSxh(int shu) {
        int ge;
        int shi;
        int bai;
        ge = shu % 10;
        shi = shu % 100;
        bai = shu / 100;
        if (ge * ge * ge + shi * shi * shi + bai * bai * bai == shu) {
            System.out.println(shu + "是水仙花数");
        } else {
            System.out.println(shu + "不是水仙花数");
        }
        return shu;
    }

    }

2、所有水仙花数

public class shuixianhua2 {
        public static void main(String[] args) {
            int a;
            Scanner py = new Scanner(System.in);
            System.out.println("1000以内的水仙花数有:");
            int ge, shi, bai;
            for(int i=1;i<1000;i++){
                bai = i / 100;
                shi = (i - bai * 100) / 10;
                ge = i - bai * 100 - shi * 10;
                if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
                    System.out.print(i + "   ");}
        }
    }

3、判断一年是否是闰年

public class runnian {
    public static void main(String[] args) {
        System.out.println("今年是哪一年");
        Scanner sc=new Scanner(System.in);
        int year=sc.nextInt();
        if(year%4==0&&year%100!=0){
            System.out.println(year+"年是闰年");
        }
        else{
            System.out.println(year+"年不是闰年");
        }
    }

4、求1990-2021年之间的所有闰年

public class runnian2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();
        int m=sc.nextInt();

        int i,temp=0;
        for(i=n;i<=m;i++){
            boolean nian=((i%4==0&&i/100!=0)||i%400==0);
            if(nian){
                System.out.println(i);
            }
            else{
                System.out.println(" ");
            }

        }
    }
}

5、分两个类写一写第4题

class runnian3{
    public boolean nian(int a){
        if (( a % 400==0 ) || ( a % 4 == 0 && a % 100 !=0 ))
            return true;
        else return false;
    }
}
class year {
    public static void main(String[] args){
        int a;
        runnian3 rn=new runnian3();
        System.out.println("1990-2021的闰年有:");
        for (int i = 1990; i <= 2021; i++) {
            if (rn.nian(i))
                System.out.print(i + "   ");
        }
    }
}

6、判断一个数是否是素数

public class sushu {
    public static void main(String[] args) {
        System.out.println("请输入一个数");
        Scanner sc=new Scanner(System.in);
        int su=sc.nextInt();
        boolean shu=true;
        for(int i=2;i<=Math.sqrt(su);i++){
            if(su%i==0){
                shu=false;
                break;
            }
        }
        if(shu)
        {
            System.out.println(su+"是素数");
        }
        else{
            System.out.println(su+"不是素数");
        }

    }
}

7、一个大于4的偶数歌德巴赫猜想

public class gedebahecaixiang1 {
    public static void main(String[] args) {
        int a=20,i=0,y,k,x=0;
        for(k=0;k<=a-2;k++){
            i=0;
            x=0;
            for(y=2;y<=k-1;y++){
                if(k%y==0) {
                    i++;
                }
                 for(y=2;y<=a-k-1;y++){
                     if((a-k)%y==0) {
                         x++;
                     }
                     if(x==0&&i==0&&k<a-k){
                         System.out.println(a+"="+k+"+"+(a-k));
                     }
                 }
            }
        }
    }
}

8、4~100之间每个偶数的歌德巴赫猜想

。public class gedebahe {
    public static void main(String[] args) {
        /*哥德巴赫猜想:一个大于2的整数都可以写成两个质数之和
        要求:一个大于四的偶数的哥德巴赫猜想
        1.求1-1000的质数
        2.如果i=质数1+质数2则i符合哥德巴赫猜想*/
        int i = 0;
        for (int su = 1; su < 101; su++) {
            if (su % 2 != 0 && su % 3 != 0 && su % 5 != 0 && su % 7 != 0) {
                for (i = 4; i % 2 == 0; i++) {
                   i=su+su;
                    if (i > 4&&i<100) {
                        System.out.println(i + "符合哥德巴赫猜想");
                    }
                }


            }
        }
    }
}

9、一个数的的因子和(包括1不包括本身)

public class yinzihe1 {
    public static void main(String[] args) {
        System.out.println("请输入一个数:");
        Scanner sc = new Scanner(System.in);
        int su = sc.nextInt();
        int sum = 1;
        for (int i = 2; i < su; i++) {
            if (su % i == 0)
                sum += i;

        }
        System.out.println(su + "的因子和是" + sum);
    }
}

10、1~10万之间的亲密数对

public class qinmishudui {

    public int x(int i){
        int j = 1,sum = 0;
        while(j <= i){
            if(i % j == 0){
                sum = sum + j;
            }
            j++;
        }
        sum = sum - i;
        return sum;
    }

    public static void main(String[] args) {
        int p,q;
        qinmishudui m = new qinmishudui();
        for(int i = 1;i <= 100000;i++){
            p = m.x(i);
            q = m.x(p);
            if(q == i && p != i && p > i){
                System.out.println(i + "和" + p + "是亲密数对");
            }
        }
    }
}

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值