重載複習和小練習

有興趣的能夠看看註釋裏面的題目要求,本身寫寫或者看看下面的用別的方式完成目標。

/*
題目要求:
比較兩個數據是否相等,
參數類型分別是byte,short,int,long
而後在main方法中測試
*/
public class HelloWorld {
    public static void main(String[] args) {
        byte x = 10;
        byte y = 20;
        System.out.println(sum(x, y));
        System.out.println(sum((short) 20, (short) 20));
        System.out.println(sum(20, 10));
        System.out.println(sum(20L,10L));
    }

    public static boolean sum(byte x, byte y) {
        if (x == y) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean sum(short x, short y) {
        boolean sum = x == y ? true : false;
        return sum;
    }
    public static boolean sum(int x, int y) {
        return x == y;
    }
    public static boolean sum(long x, long y) {
        boolean max;
        if (x == y) {
            max = true;
        } else {
            max = false;
        }
        return max;
    }
}
/*
題目要求:
比較兩個數據是否相等,
參數類型分別是byte,short,int,long
而後在main方法中測試
*/
public class HelloWorld {
    public static void main(String[] args) {
        byte x = 10;
        byte y = 20;
        System.out.println(sum(x, y));
        System.out.println(sum((short) 20, (short) 20));
        System.out.println(sum(20, 10));
        System.out.println(sum(20L,10L));
    }

    public static boolean sum(byte x, byte y) {
        if (x == y) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean sum(short x, short y) {
        boolean sum = x == y ? true : false;
        return sum;
    }
    public static boolean sum(int x, int y) {
        return x == y;
    }
    public static boolean sum(long x, long y) {
        boolean max;
        if (x == y) {
            max = true;
        } else {
            max = false;
        }
        return max;
    }
}

相關文章
相關標籤/搜索