我不太明白如何让这些 public 整数工作

I cant quite figure out how to get these public integers to work

PUBLIC CLASS

public class MonthDays {

        private int month;
        private int year;

        public int Month(int x){
            month = x;
            return x;
        }
        public int Year(int y){
            year = y;
            return y;
        }

    public int getNumberOfDays(){

        if      (month == 1 || month == 3 || month == 5 ||          //determines if the the month entered is a month with 31 days or not
                month == 7 || month == 8 ||month == 10 || month == 12){
            return 31;
        }
        if (year % 4 == 0 && month == 2){           //determines if the month is february on a leap year
            return 29;
        }
        if (year % 4 != 0 && month ==2){            //determines if the month is february on a non leap year

            return 28;
        }
        else{                                       //any other condition is a month with 30 days
            return 30;
        }

    }
}

DRIVER CLASS

import java.util.Scanner;

public class MonthDaysDriver {

    public static void main(String[] args) {

        int x;
        int y;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter month: ");
        Month month = new Month(reader.nextInt());
        System.out.println("Enter year: ");
        Year year = new Year(reader.nextLine());




    }

}

我不太确定哪里出了问题,但据我了解,这应该可行。但我也很确定这是某个地方的愚蠢错误。

您正在测试的 class 是 MonthDays。您有方法 MonthYear(可能应该称为 setMonthsetYear)。

MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.Month(reader.nextInt());
System.out.println("Enter year: ");
md.Year(reader.nextInt());

或者,如果您重命名这些方法,

MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.setMonth(reader.nextInt());
System.out.println("Enter year: ");
md.setYear(reader.nextInt());

或者,向 MonthDays 添加一个构造函数,它接受 monthyear

public MonthDays(int month, int year) {
   this.month = month;
   this.year = year;
}

并称其为

System.out.println("Enter month: ");
int month = reader.nextInt();
System.out.println("Enter year: ");
int year = reader.nextInt();
MonthDays md = new MonthDays(month, year);

最后,您可以在拥有 MonthDays 实例后调用 getNumberOfDays。喜欢

System.out.println(md.getNumberOfDays());

这应该能让您的代码正常工作。请阅读 Object-Oriented 编程中的构造函数。

public class

public class MonthDays {

        private int month;
        private int year;

        public MonthDays(int x, int y){
            month = x;
            year = y;
        }

        public int getNumberOfDays(){

            if (month == 1 || month == 3 || month == 5 month == 7 || month == 8 ||month == 10 || month == 12){ //determines if the the month entered is a month with 31 days or not
            return 31;
            }
            if (year % 4 == 0 && month == 2){           //determines if the month is february on a leap year
            return 29;
            }
            if (year % 4 != 0 && month ==2){            //determines if the month is february on a non leap year    
            return 28;
            }
            else{                                       //any other condition is a month with 30 days
            return 30;
            }

        }
    }

driver class

import java.util.Scanner;

public class MonthDaysDriver {

    public static void main(String[] args) {

        int x;
        int y;
        int days;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter month: ");
        x = reader.nextInt();
        System.out.println("Enter year: ");
        y = reader.nextInt();
        MonthDays md = new MonthDays(x, y);
        days = md.getNumberOfDays();
        System.out.println("Number of days= " + days);




    }

}

嗨,您的代码中犯了一些基本错误。

1) Month 是一个方法,而不是 class.You 应该需要 MonthDays 的实例来调用方法 Month 。 根据 java 编码标准,您应该将其编写为 setMonth() 或 setYear() 2) 方法 Month return int value.You 应该已将其分配给 int 值。

3) Argument of Year 方法也是一个 int value.You 已经指定了一个 String 值 now

public class MonthDays 
{
    private int month;
    private int year;

    public int setMonth(int x){
        month = x;
        return x;
    }
    public int setYear(int y){
        year = y;
        return y;
    }

public int getNumberOfDays(){

    if      (month == 1 || month == 3 || month == 5 ||          //determines if the the month entered is a month with 31 days or not
            month == 7 || month == 8 ||month == 10 || month == 12){
        return 31;
    }
    if (year % 4 == 0 && month == 2){           //determines if the month is february on a leap year
        return 29;
    }
    if (year % 4 != 0 && month ==2){            //determines if the month is february on a non leap year

        return 28;
    }
    else{                                       //any other condition is a month with 30 days
        return 30;
    }

}
}


import java.util.Scanner;

public class MonthDaysDriver {

    public static void main(String[] args) {

        int x;
        int y;
        MonthDays MD=new MonthDays();
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter month: ");
        int month = MD.setMonth(reader.nextInt());
        System.out.println("Enter year: ");
        int year = MD.setYear(reader.nextInt());
        int numberOfDays=MD.getNumberOfDays();
        System.out.println("Number oF days in that month :"+numberOfDays);


    }

}