检查数字是奇数还是偶数,使用getter和setter

时间:2019-02-07 17:27:32

标签: java methods

此方法有效,但不显示奇数

public class OddEvan_checker {

    private int number;

    public void setNumber(int number) {
        number = number;
    }

    public int getNumber() {
        return number;
    }

    public int calculate() {

        if (number % 2 == 0) {
            System.out.println(number+"even");

        } else {
            System.out.println("odd ");

        }
        return number;
    }
}

1 个答案:

答案 0 :(得分:0)

public class OddEvenChecker {  // Also consider renaming the typo and avoiding snake_case
    private int number;
    public void setNumber(int number) {
        number = number;
    }

    public int getNumber() {
        return number;
    }

    public int calculate() {
        if (number % 2 == 0) {
            System.out.println(number+"even");
        } else {
            System.out.println(number+"odd ");  // add the number here to solve the issue
        }
        return number;
    }
}