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;
}
}
}
驾驶员课程
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());
}
}
我不太确定我哪里出错,但从我的理解,这应该是有效的。但我也很确定它在某处是一个愚蠢的错误。
答案 0 :(得分:1)
您正在测试的课程是MonthDays
。您有方法Month
和Year
(可能应称为setMonth
和setYear
)。
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
month
和year
。{/ p>
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);
最后,您可以在getNumberOfDays
个实例后调用MonthDays
。像
System.out.println(md.getNumberOfDays());
答案 1 :(得分:1)
这应该让您的代码正常工作。请阅读面向对象编程中的构造函数。
公共课
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;
}
}
}
驱动程序类
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);
}
}
答案 2 :(得分:1)
Hai You在您的代码中犯了一些基本错误。
1)Month是一个方法,而不是一个类。你应该需要一个MonthDays实例来调用方法Month。 根据java编码标准,您应该将其写为setMonth()或setYear() 2)Method Month返回一个int值。你应该将它赋给int值。
3)Year方法的参数也是一个int值。你现在已经指定了一个String值
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);
}
}