栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于偶数,奇数,素数调用方法类型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于偶数,奇数,素数调用方法类型

一类

package experiment;

public class MyInteger {
    private int value;
    public MyInteger(int value) {
        this.value = value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public int getValue() {
        return this.value;
    }
    //无参判断是否为偶数
    public String isEven() {
        if(this.value%2==0) {
            return "true";
        }else {
            return "false";
        }    
    }
    //无参判断是否为奇数
    public String isOdd() {
        if(this.value%2==1) {
            return "true";
        }else {
            return "false";
        }
    }
    //无参判断是否为素数
    public String isPrime() {
        int i;
        for(i = 2;i             if(this.value%i==0) {
                break;
            }
        }
        if(i == this.value) {
            return "true";
        }else {
            return "false";
        }
    }
    //静态传整形判断是否为偶数
    public static String isEven(int value) {
        if(value%2==0) {
            return "true";
        }else {
            return "false";
        }
    }
    //静态传整形判断是否为奇数
    public static String isOdd(int value) {
        if(value%2==1) {
            return "true";
        }else {
            return "false";
        }
    }
    //静态传整形判断是否为素数
    public static String isPrime(int value) {
        int i;
        for(i = 2;i             if(value%i==0) {
                break;
            }
        }
        if(i == value) {
            return "true";
        }else {
            return "false";
        }
    }
    //静态传对象判断是否为偶数
    public static String isEven(MyInteger myInteger) {
        if(myInteger.value%2==0) {
            return "true";
        }else {
            return "false";
        }    
    }
    //静态传对象判断是否为奇数
    public static String isOdd(MyInteger myInteger) {
        if(myInteger.value%2==1) {
            return "true";
        }else {
            return "false";
        }
    }
    //静态传对象判断是否为素数
    public static String isPrime(MyInteger myInteger) {
        int i;
        for(i = 2;i             if(myInteger.value%i==0) {
                break;
            }
        }
        if(i == myInteger.value) {
            return "true";
        }else {
            return "false";
        }

    }
    //传整形判断是否相等
    public String equals(int value) {
        if(this.value == value) {
            return "true";
        }else {
            return "false";
        }
    }
    //传对象判断是否等值
    public String equals(MyInteger myInteger) {
        if(this.value == myInteger.value) {
            return "true";
        }else {
            return "false";
        }
    }
    

}

测试类

package experiment;

import java.util.Scanner;

public class MyIntegerTest {
    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        MyInteger n1 = new MyInteger(input.nextInt());
        System.out.println("n1是偶数吗?"+"t"+n1.isEven());
        System.out.println("n1是素数吗?"+"t"+n1.isPrime());
        System.out.println("n1是素数吗?"+"t"+MyInteger.isPrime(n1));
        
        MyInteger n2 = new MyInteger(input.nextInt());
        System.out.println("n2是奇数吗?"+"t"+n2.isOdd());
        System.out.println("45是奇数吗?"+"t"+MyInteger.isOdd(45));
        System.out.println("n1与n2相等吗?"+"t"+n1.equals(n2));
        System.out.println("n1等于5吗?"+"t"+n1.equals(5));
        input.close();
    }

}

结果

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号