Печать дня недели с даты ввода

#java

Вопрос:

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

 import java.util.Scanner;

//declare variables and methods
class Main {
  Scanner scan = new Scanner(System.in);
  String date, answer = "yes";
  int remainder, month, day, year, days, leapYear;

  public void runProgram()
  {
    while(answer.equalsIgnoreCase("yes"))
    {
      input();
      calculateDays();
      output();

      days = 0;

      System.out.println("n"   "Do you want to run the program again?");
      answer = scan.nextLine();
    }
  }

  //split date into month, day, year and convert to integers
  public void input()
  {
    System.out.println("n"   "mm/dd/yyyy:");
    date = scan.nextLine();

    month = Integer.valueOf(date.substring(0, 2));
    day = Integer.valueOf(date.substring(3, 5));
    year = Integer.valueOf(date.substring(6));
    leapYear = Integer.valueOf(date.substring(6, 8));

    System.out.println(leapYear);
    System.out.println(day);
    System.out.println(year);
  }

  //calculate the total amount of days since 01/01/0000
  public void calculateDays()
  {
    days  = day;

    //different no. of days pass per month
    switch(month)
    {
      case 1:
        days  = 0;
        System.out.println("n"   "Month: January/31 days");
        break;
      case 2:
        days  = 31;
        System.out.println("n"   "Month: February/28 days");
        break;
      case 3:
        days  = 59;
        System.out.println("n"   "Month: March/31 days");
        break;
      case 4:
        days  = 90;
        System.out.println("n"   "Month: April/30 days");
        break;
      case 5:
        days  = 120;
        System.out.println("n"   "Month: May/31 days");
        break;
      case 6:
        days  = 151;
        System.out.println("n"   "Month: June/30 days");
        break;
      case 7:
        days  = 181;
        System.out.println("n"   "Month: July/31 days");
        break;
      case 8:
        days  = 212;
        System.out.println("n"   "Month: August/31 days");
        break;
      case 9:
        days  = 243;
        System.out.println("n"   "Month: September/30 days");
        break;
      case 10:
        days  = 273;
        System.out.println("n"   "Month: October/31 days");
        break;
      case 11:
        days  = 304;
        System.out.println("n"   "Month: November/30 days");
        break;
      case 12:
        days  = 334;
        System.out.println("n"   "Month: December/31 days");
    }

    days  = (year-1)*365;

    days -= leapYear;
    if(leapYear%4 == 0){
      days  = (int)(leapYear/4);
    }

    //leap days
    if(year%4 == 0  amp;amp;  month <= 2){
      days  = (int)((year-1)/4);
    }
    else{
        days  = (int)(year/4);
    }
  }

  //calculate and output the day of the week
  public void output()
  {
    remainder = days % 7;
    switch(remainder)
    {
      case 0:
        System.out.println("Day of the week: Saturday");
        break;
      case 1:
        System.out.println("Day of the week: Sunday");
        break;
      case 2:
        System.out.println("Day of the week: Monday");
        break;
      case 3:
        System.out.println("Day of the week: Tuesday");
        break;
      case 4:
        System.out.println("Day of the week: Wednesday");
        break;
      case 5:
        System.out.println("Day of the week: Thursday");
        break;
      case 6:
        System.out.println("Day of the week: Friday");
        break;
    }
  }


  public static void main(String[] args) {
    Main prog = new Main();
    prog.runProgram();
  }
}
 

Комментарии:

1. любая причина, по которой вы не хотите использовать импорт java.time. День недели; в встроенной библиотеке ? geeksforgeeks.org/…

Ответ №1:

прокомментируйте эти строки кода, которые вы уже обрабатываете для високосного года, а также инициализируйте дни =0 в начале

 days -= leapYear;
    if(leapYear%4 == 0){
      days  = (int)(leapYear/4);
    }
 

ниже приведен окончательный код

 import java.util.Scanner;

//declare variables and methods
public class Main {
  Scanner scan = new Scanner(System.in);
  String date, answer = "yes";
  int remainder, month, day, year, days=0, leapYear;

  public static void main(String[] args) {
    Main prog = new Main();
    prog.runProgram();
  }
  public void runProgram()
  {
    while(answer.equalsIgnoreCase("yes"))
    {
      input();
      calculateDays();
      output();

      days = 0;

      System.out.println("n"   "Do you want to run the program again?");
      answer = scan.nextLine();
    }
  }

  //split date into month, day, year and convert to integers
  public void input()
  {
    System.out.println("n"   "mm/dd/yyyy:");
    date = scan.nextLine();

    month = Integer.valueOf(date.substring(0, 2));
    day = Integer.valueOf(date.substring(3, 5));
    year = Integer.valueOf(date.substring(6));
    leapYear = Integer.valueOf(date.substring(6, 8));

    System.out.println(leapYear);
    System.out.println(day);
    System.out.println(year);
  }

  //calculate the total amount of days since 01/01/0000
  public void calculateDays()
  {
    days  = day;

    //different no. of days pass per month
    switch(month)
    {
      case 1:
        days  = 0;
        System.out.println("n"   "Month: January/31 days");
        break;
      case 2:
        days  = 31;
        System.out.println("n"   "Month: February/28 days");
        break;
      case 3:
        days  = 59;
        System.out.println("n"   "Month: March/31 days");
        break;
      case 4:
        days  = 90;
        System.out.println("n"   "Month: April/30 days");
        break;
      case 5:
        days  = 120;
        System.out.println("n"   "Month: May/31 days");
        break;
      case 6:
        days  = 151;
        System.out.println("n"   "Month: June/30 days");
        break;
      case 7:
        days  = 181;
        System.out.println("n"   "Month: July/31 days");
        break;
      case 8:
        days  = 212;
        System.out.println("n"   "Month: August/31 days");
        break;
      case 9:
        days  = 243;
        System.out.println("n"   "Month: September/30 days");
        break;
      case 10:
        days  = 273;
        System.out.println("n"   "Month: October/31 days");
        break;
      case 11:
        days  = 304;
        System.out.println("n"   "Month: November/30 days");
        break;
      case 12:
        days  = 334;
        System.out.println("n"   "Month: December/31 days");
    }

    days  = (year-1)*365;

    if(year%4 == 0  amp;amp;  month <= 2){
      days  = (int)((year-1)/4);
    }
    else{
        days  = (int)(year/4);
    }
  }

  //calculate and output the day of the week
  public void output()
  {
    remainder = days % 7;
    switch(remainder)
    {
      case 0:
        System.out.println("Day of the week: Saturday");
        break;
      case 1:
        System.out.println("Day of the week: Sunday");
        break;
      case 2:
        System.out.println("Day of the week: Monday");
        break;
      case 3:
        System.out.println("Day of the week: Tuesday");
        break;
      case 4:
        System.out.println("Day of the week: Wednesday");
        break;
      case 5:
        System.out.println("Day of the week: Thursday");
        break;
      case 6:
        System.out.println("Day of the week: Friday");
        break;
    }
  }


  
}