«Как исправить« класс, интерфейс или enum ожидаемый »для моего кода - PullRequest
0 голосов
/ 20 мая 2019

Я не могу найти решение для своего кода, я получаю «ожидаемый класс, интерфейс или перечисление» при попытке скомпилировать мой код.

Я пытался выяснить, как решить проблемуно я, к сожалению, не смог этого сделать, так как кажется, что у меня ничего не работает ... также, если есть какие-то ошибки, которые могут привести к тому, что он не будет работать после исправления этой части, пожалуйста, дайте мне знать, что я могу изменить!

Код:

class MyDate {

//properties of date object
private int day, month, year;

//Constructor with arguments
public MyDate(int day, int month, int year) {
   this.day = day;
   this.month = month;
   this.year = year;
}

public boolean isValidDate() {
   if (month > 12 || month < 1 || day < 1) { // if negative values found
       return false;
   } else if (year <= 1582 && month <= 10 && day <= 15) { // starting date
                                                           // checking
       return false;
   } // for 31 day months
   else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

       if (day > 31) {
           return false;
       }
   } // for 30 day months
   else if (month == 4 || month == 6 || month == 9 || month == 11) {

       if (day > 30) {
           return false;
       }

   } else if (month == 2) // February check
   {
       if (isLeapYear()) // Leap year check for February
       {
           if (day > 29) {
               return false;
           }
       } else {
           if (day > 28) {
               return false;
           }
       }
   }

   return true;
   }

   // checks if this year is leap year
   private boolean isLeapYear() {
   if (year % 4 != 0) {
       return false;
   } else if (year % 400 == 0) {
       return true;
   } else if (year % 100 == 0) {
       return false;
   } else {
       return true;
   }
 }

 /**
 * @return the day
 */
 public int getDay() {
   return day;
 }

 /**
 * @param day
 *            the day to set
 */
 public void setDay(int day) {
   this.day = day;
}

/**
* @return the month
*/
public int getMonth() {
   return month;
}

/**
* @param month
*            the month to set
*/
public void setMonth(int month) {
   this.month = month;
}

/**
* @return the year
*/
public int getYear() {
   return year;
}

/**
* @param year
*            the year to set
*/
public void setYear(int year) {
   this.year = year;
}

@Override
public String toString() {
   return day + "/" + month + "/" + year;
  }
} 

import java.util.Scanner;

public class MyCalendar {

//enums for days of week
public static enum Day {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
};

//enum for month of year
public static enum Month {
   JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, 
 OCTOBER, NOVEMBER, DECEMBER;
};

//enums for week numbers
public static enum Week { FIRST, SECOND, THIRD, FOURTH;
};

//to store Date object
private MyDate date;

//constructor taking mydate object
public MyCalendar(MyDate enteredDate) {
   this.date = enteredDate;
}

 //main method
public static void main(String[] args) {

   boolean validDate = false; //valid date false
   Scanner input = new Scanner(System.in); //scanner for input
   MyDate enteredDate = null;

   //till valid date found
   while (!validDate) {
       System.out.print("Enter the date as day month year : ");
       //taking input and creating date object
       enteredDate = new MyDate(input.nextInt(), input.nextInt(), 
 input.nextInt());
       //validdating date object
       if (enteredDate.isValidDate()) { //if valid
           MyCalendar myCalendar = new MyCalendar(enteredDate); //creating 
calendar object
           myCalendar.printDateInfo(); //printing date info
           myCalendar.printCalendar(); //printing calendar
           validDate = true; //setting validate to true 
       } else {
           System.out.println("Please enter a Valid Date...");
       }
   }

   input.close();
 }

// returns number of days in current month
private int getNumberOfDays() {
   int days = 31;
   int month = date.getMonth();
   if (month == 4 || month == 6 || month == 9 || month == 11)
       days = 30;
    return days;
}

//print calendar of this month
public void printCalender() {
   System.out.println("\n\nThe Calendar of "+Month.values() 
 [date.getMonth()-1]+" "+date.getYear()+" is :");
   int numberOfMonthDays = getNumberOfDays();
   Day firstWeekdayOfMonth = getDayOfWeek(1, date.getMonth(), 
 date.getYear());
   int weekdayIndex = 0;
   System.out.println("SUN MON TUE WED THU FRI SAT"); // The order of days
                                                       // depends on your
                                                       // calendar

   for (int day = 0; Day.values()[day] != firstWeekdayOfMonth; day++) {
       System.out.print("    "); // this loop to print the first day in 
his
                                   // correct place
       weekdayIndex++;
   }
   for (int day = 1; day <= numberOfMonthDays; day++) {

       if (day < 10) 

           System.out.print(day + " ");
       else
           System.out.print(day);
       weekdayIndex++;
       if (weekdayIndex == 7) {
           weekdayIndex = 0;
           System.out.println();
       } else {
           System.out.print(" ");
       }
   }
   System.out.println();
 }

//method to print about date information in literal form
public void printDateInfo() {
   System.out.println(date + " is a " + getDayOfWeek(date.getDay(), 
date.getMonth(), date.getYear())
           + " located in the " + Week.values()[getWeekOfMonth() - 1] + " 
week of "
           + Month.values()[date.getMonth() - 1] + " " + date.getYear());
}

/*
* gets day of the week, returns enum type Day
*
* day of week (h) = (q+(13*(m+1)/5)+K+(K/4)+(J/4)+5J)%7 ,q- day of month,
* m- month, k = year of century (year%100), J = (year/100)
*/
public Day getDayOfWeek(int day, int month, int year) {
   int q = day;
   int m = month;

   if (m < 3) {
       m = 12 + date.getMonth();
       year = year - 1;
   }
   int K = year % 100;
   int J = year / 100;
   //calculating h value
   int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
   Day output = null;
   if (h < Day.values().length && h >= 0) {
       output = Day.values()[h - 1]; //getting respective enum value
   }
   return output; //returning enum value
 }

// get week number of current date
public int getWeekOfMonth() {
   int days = date.getDay();
   int weeks = days / 7;
   days = days % 7;
   if (days > 0)
       weeks++;
   return weeks;
  }

}

Ошибка:

    MyCalendar.java:120: class, interface, or enum expected
    import java.util.Scanner;
    ^

Ошибка при перемещении импорта наверх (ОБНОВЛЕНО):

MyCalendar.java:164: cannot find symbol
symbol  : method printCalendar()
location: class MyCalendar
               myCalendar.printCalendar(); //printing calendar

Ожидаемый код:

java MyCalendar 29/02/2019
29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019
25/05/2019 is a Saturday and located in the fourth week of May 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
            1   2   3   4  
5   6   7   8   9   10  11
12  13  14  15  16  17  18
19  20  21  22  23  24  25
26  27  28  29  30  31

1 Ответ

1 голос
/ 20 мая 2019

метод public void printCalender() должен вызываться или вызываться как printCalender(), а не как printCalendar()

Ваш метод printCalender(), тогда как вы вызываете printCalendar(), который не существует.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...