设计一个MyInteger类判断为偶数/奇数/素数(练习构造方法)

//设计一个MyInteger类,这个类包括:一个名为value的int类型字段,存储这个对象表示的整数值:如果当前value值分别是偶数/奇数/素数,那么isEven()/isOdd()/isPrime()方法返回true;如果指定值分别是偶数/奇数/素数,那么isEven(int)/isOdd(int)/isPrime(int)方法返回true

代码如下:

import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {
        MyInteger integer1=new MyInteger(11);
        System.out.println(integer1.isOdd());
        
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        MyInteger integer2=new MyInteger();
        System.out.print(integer2.isEven(num));
    }

}

//MyInteger类

public class MyInteger {
    int value;
    
    //构造方法
    public MyInteger(int value){
        this.value=value;
    }
    public MyInteger(){
    }
    
    public boolean isEven(){
        if(value%2==0)
            return true;
        else
            return false;
    }
    
    public boolean isOdd(){
        if(value%2==0)
            return false;
        else
            return true;
    }
    
    public boolean isPrime(){
        for(int x=2;x<value-1;x++){
            if(value%x==0)
                return false;
        }
        return true;
    }
    
    public boolean isEven(int num){
        if(num%2==0)
            return true;
        else
            return false;
    }
    
    public boolean isOdd(int num){
        if(num%2==0)
            return false;
        else
            return true;
    }
    
    public boolean isPrime(int num){
        for(int x=2;x<num-1;x++){
            if(num%x==0)
                return false;
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值