简体   繁体   English

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

[英]How to print a non-static method from a static method?

I need to print a calendar of a given input from the command line.我需要从命令行打印给定输入的日历。 The main problem I am having is trying to print the actual body of the calendar.我遇到的主要问题是尝试打印日历的实际正文。 I have a method:我有一个方法:

printMonth(); 

that is used to print the calendar.用于打印日历。 The method is void and non-static.该方法是无效的和非静态的。 However, when I call the method from main (static), there is no output.但是,当我从 main(静态)调用方法时,没有 output。

Here is my code so far:到目前为止,这是我的代码:

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();


    }


}

I am quite new to java, so any assistance with this problem would be greatly appreciated!我对 java 还是很陌生,所以对于这个问题的任何帮助将不胜感激!

In your method printMonth() you have called method getNumberOfDaysInMonth(theMonth, theYear) and you have passed arguments in wrong order在您的方法printMonth()中,您调用了方法getNumberOfDaysInMonth(theMonth, theYear)并且您以错误的顺序通过了 arguments

change this line:改变这一行:

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

to this one:对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM