Здравствуйте, у меня небольшая проблема с этим кодом - PullRequest
1 голос
/ 29 апреля 2020
private int calculateWeekDay() {
     if (this.month == 1 || this.month == 2) {
        this.month += 12;
        this.year -= 1;
     }
     int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
     String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
     int [] Array = new int[weekDay.length];
     for(int i =0 ; i < weekDay.length ; i++){
        Array[i] = Integer.parseInt(weekDay[i]);
     }
     return h;
}

Я действительно не знаю, где ошибка, и все, что я получаю при отладке, это ошибка: Исключение в потоке "main" java .lang.NumberFormatException: Для строки ввода: "Monday" at java .lang.NumberFormatException.forInputString (NumberFormatException. java: 65) в java .lang.Integer.parseInt (Integer. java: 580) в java .lang.Integer.parseInt (Integer. java: 615) по адресу javaapplication22.JavaApplication22 $ Zeller.calculateWeekDay (JavaApplication22. java: 112) по адресу javaapplication22.JavaApplication22 $ Zeller.toString (JavaApplication22. java: 125) по адресу javaapplication22.JavaApplication22.JavaApplication22. *: 17) C: \ Users \ salee \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run. xml: 53: Java возвращено: 1 BUILD FAILED (общее время: 5 секунд)

И это весь код, если он поможет найти проблему:

package javaapplication22;
import java.util.Scanner;

    public class JavaApplication22 {



     public static void main(String[] args) {
            Scanner scan = new Scanner (System.in);
            Zeller [] obj = new Zeller[5];

            for (int i = 1 ; i <= obj.length ; i++){

                System.out.print("Enter Valid year (>0) - Valid month [1-12] and Valid day [1-31] for Object " + (i) + ":");
                int year = scan.nextInt();
                int month = scan.nextInt();
                int day = scan.nextInt();
                obj[i] = new Zeller(year , month , day);
                System.out.println(obj[i].toString());

            }


        }

        public static class Zeller {

            private int year;
            private int month;
            private int day;
            private String weekDay;
            private static int countLeapYears;

            public Zeller() {
                year = -1;
                month = -1;
                day = -1;
            }

            public Zeller(int y, int m, int d) {
                this.year = y;
                this.month = m;
                this.day = d;

            }

            public int getYear() {
                return year;
            }

            public int getMonth() {
                return month;
            }

            public int getDay() {
                return day;
            }

            public String getWeekDay() {
                return weekDay;
            }

            public static int getCountLeapYears() {
                return countLeapYears;
            }

            public void setYear(int y) {
                if (y > 0) {
                    System.out.println(y);
                } else if( y <= 0){
                    System.out.println(this.year);
                }
            }

            public void setMonth(int m) {
                if (m >= 1 && m <= 12) {
                    System.out.println(m);
                } else if (!(m >= 1 && m <=12  )){
                    System.out.println(this.month);
                }
            }

            public void setDay(int m) {
            if( m == 1 || m == 3 ||m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
                 this.day = 31;
                 System.out.println(this.day );
            }else if( m == 4 || m == 6 || m == 9 || m == 11){
                this.day = 30;
                System.out.println(this.day);

            }else if(m == 2){
                if(this.checkLeapYear(year) == true){
                    this.day = 29;
                    System.out.println(this.day);
                }else {
                    this.day = 28;
                    System.out.println(this.day);
                }
            }else{
                System.out.println(this.day);
            }

            }

            private int calculateWeekDay() {
                if (this.month == 1 || this.month == 2) {
                    this.month += 12;
                    this.year -= 1;
                }
                int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
                String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
                int [] Array = new int[weekDay.length];
                for(int i =0 ; i < weekDay.length ; i++){
                    Array[i] = Integer.parseInt(weekDay[i]);
                }
                return h;

            }

            public boolean checkLeapYear(int year) {
                return true == (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
            }

            @Override
            public String toString() {
                if(this.year != -1 || this.month !=0 ||this.day !=0){
                    return (" In " + this.getYear() + " / " + this.getMonth() + " / " + this.getDay() + " the day was: " + calculateWeekDay());
                }
                return ("The Date in this Object was input wrong");
            }



            }

    }
}

Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 29 апреля 2020

попробуйте

if (this.month == 1 || this.month == 2)
{
    this.month = (this.month == 1) ? 13 : 14;
    this.year--;
}

int h = (this.day + (26 * (this.month + 1)) / 10 + (this.year % 100) + (this.year % 100) / 4 + (this.year / 100) / 4 + 5 * (this.year / 100)) % 7;

1: воскресенье 2: понедельник 3: вторник 4: среда 5: четверг 6: пятница 7: суббота

0 голосов
/ 29 апреля 2020

Скорее всего, ваш код должен вычислять String название дня недели:

static final String[] WEEKDAY = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};

private String calculateWeekDay() {
        if (this.month == 1 || this.month == 2) {
            this.month += 12;
            this.year -= 1;
        }
        int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % WEEKDAY.length;

        return weekDay[h];
    }

Но вы можете упустить что-то еще, потому что на сегодняшнюю дату 2020-04-29 (среда) возвращается Sunday

...