如何从静态方法打印非静态方法?

问题描述 投票:0回答:1

我需要从命令行打印给定输入的日历。我遇到的主要问题是尝试打印日历的实际正文。我有一个方法:

printMonth(); 

用于打印日历。该方法是无效且非静态的。但是,当我从main(静态)调用该方法时,没有输出。

到目前为止是我的代码:

class MyDate {

private int day;
private int month;
private int year;

public MyDate(int theDay, int theMonth, int theYear){

    theDay = day;

    theMonth = month;

    theYear = year;

}

public MyDate(){

}

public int getDay() {

    return day;
}

public int getMonth() {

    return month;
}

public String nameOfMonth() {
    switch (month) {
    case 1:
        return "January";
    case 2:
        return "February";
    case 3:
        return "March";
    case 4:
        return "April";
    case 5:
        return "May";
    case 6:
        return "June";
    case 7:
        return "July";
    case 8:
        return "August";
    case 9:
        return "September";
    case 10:
        return "October";
    case 11:
        return "November";
    case 12:
        return "December";
    // for any number less than 1 and greater than 12 throw an exception
    default:
        throw new RuntimeException("Invalid month number");
    }
}

public int getYear() {

    return year;
}



public String getDate(String[] datePartsObj) {

        day = Integer.parseInt(datePartsObj[0]);
        month = Integer.parseInt(datePartsObj[1]);
        year = Integer.parseInt(datePartsObj[2]);

        return day + "/" + month + "/" + year ;

}


  public int getStartDay(int theYear, int theMonth) {

      year = theYear;
      month = theMonth;

        // Get total number of days since 1/1/1800
        int startDay1800 = 3;

        int totalNumberOfDays = getTotalNumberOfDays(theYear, theMonth);



        // Return the start day
        return (totalNumberOfDays + startDay1800) % 7;

      }


public int getTotalNumberOfDays(int theYear, int theMonth) {

    year = theYear;

    month = theMonth;

    int total = 0;



    // Get the total days from 1800 to year - 1
    for (int i = 1800; i < theYear; i++)

    if (isLeapYear(i))

      total = total + 366;

    else

      total = total + 365;



    // Adding days from January to the month prior to the calendar month
    for (int i = 1; i < theMonth; i++)

      total = total + getNumberOfDaysInMonth(theYear, i);



    return total;

  }


 public int getNumberOfDaysInMonth(int theMonth, int theYear) {

     month = theMonth; 

     year = theYear;

        if (theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 ||

                theMonth == 8 || theMonth == 10 || theMonth == 12)

          return 31;



        if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11)

          return 30;



        if (theMonth == 2) return isLeapYear(theYear) ? 29 : 28;



        return 0; // If month is incorrect
      }


 public boolean isLeapYear(int theYear) {

     year = theYear;

        if  ((theYear % 4 == 0) && (theYear % 100 != 0)) return true;
        if  (theYear % 400 == 0) return true;
        return false;
    }

}


public class MyCalendar {

private static MyDate myDateObj;


public enum WeekDay{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday;
}

public MyCalendar(MyDate newdate){



}

public MyCalendar(){

}

public int weekOfMonth() {

    return;

}

public void printMonth() {

    int theYear = myDateObj.getYear();

    int theMonth = myDateObj.getMonth();

    // Get start day of the week for the first date in the month
    int startDay = myDateObj.getStartDay(theYear, theMonth);

    // Get number of days in the month
    int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theYear, theMonth);

    // Pad space before the first day of the month
    int i = 0;

    for (i = 0; i < startDay; i++)

      System.out.print("    ");



    for (i = 1; i <= numberOfDaysInMonth; i++) {

      if (i < 10)

        System.out.print("   " + i);

      else

        System.out.print("  " + i);



      if ((i + startDay) % 7 == 0)

        System.out.println();

    }



    System.out.println();


}

public static void main(String [] args) {

    String[] datePartsObj;
    datePartsObj = args[0].split("/");
    int day = Integer.parseInt(datePartsObj[0]);
    int month = Integer.parseInt(datePartsObj[1]);
    int year = Integer.parseInt(datePartsObj[2]);



    myDateObj = new MyDate(day, month, year);
    MyCalendar myCalendar = new MyCalendar();


    System.out.println(myDateObj.getDate(datePartsObj) + " is a " + " and locates in the " + myDateObj.nameOfMonth());

    myDateObj.nameOfMonth();

    System.out.println("The calendar of " + myDateObj.nameOfMonth() + " " + myDateObj.getYear() + " is: ");

    System.out.println();

    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

    myCalendar.printMonth();


    }


}

我对Java还是很陌生,因此非常感谢您解决此问题!

java command-line static-methods non-static
1个回答
0
投票

在您的方法printMonth()中,您已经调用了方法getNumberOfDaysInMonth(theMonth, theYear),并且您以错误的顺序传递了参数

更改此行:

int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theYear, theMonth);

至此:

int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theMonth, theYear);
© www.soinside.com 2019 - 2024. All rights reserved.