2013-06-19 4 views
0

계산기는 생일이 이번 달 이후의 달인 경우에만 올바른 값을 반환합니다. 나는 이것을 잠시 고민하고 있는데, 나는이 논리 오류를 고치는 법을 모른다. 제발, 어떤 도움을 많이 주시면 감사하겠습니다.자바 생일 계산기 논리 오류

import java.util.*; 

public class BirthdayCalculator { 

    public static void main(String[] args) { 
     System.out.println("Enter the current month numerically (i.e. if it is July, enter 7)"); 
     Scanner console = new Scanner (System.in); 
     int monthIs = console.nextInt(); 
     System.out.println("Enter today's date"); 
     int todayIs = console.nextInt(); 
     System.out.println("Enter person 1's birth month"); 
     int birthMonthOne = console.nextInt(); 
     System.out.println("Enter person 1's birthday"); 
     int birthDayOne = console.nextInt(); 
     System.out.println("Enter person 2's birth month"); 
     int birthMonthTwo = console.nextInt(); 
     System.out.println("Enter person 2's birthday"); 
     int birthDayTwo = console.nextInt(); 
     int daysUntilEndOfMonth = getMonth(monthIs, todayIs, 0); 
     int totalDaysOne = monthsUntilBirthMonth(monthIs, birthMonthOne, 0); 
     int totalDaysTwo = monthsUntilBirthMonth(monthIs, birthMonthTwo, 0); 
     finalReport(daysUntilEndOfMonth, totalDaysOne, totalDaysTwo, birthDayOne, birthDayTwo); 
    } 

    public static int getMonth(int monthIs, int todayIs, int daysUntilEndOfMonth) { 
     if (monthIs == 2) { 
      daysUntilEndOfMonth = 28 - todayIs; //days until end of february 
     } else if (monthIs == 1 || monthIs == 3 || monthIs == 5 || monthIs == 7 || monthIs == 8 || monthIs == 10 || monthIs == 12) { 
      daysUntilEndOfMonth = 31 - todayIs; //days until end of months with 31 days 
     } else { 
      daysUntilEndOfMonth = 30 - todayIs; //days until end of month with 30 days 
     } 
     return daysUntilEndOfMonth; 
    } 

    public static int monthsUntilBirthMonth(int initialMonth, int endMonth, int totalDays) { 
     if (initialMonth == 12) { // makes sure that largest month is december 
      initialMonth = 1; 
     } else { 
      initialMonth = initialMonth + 1; 
     } 
     if (initialMonth < endMonth) { 
      for (int i = initialMonth; i < endMonth; i++) { //adds total days until birthday until month of birthday is reached 
      if (i == 2) { 
       totalDays = totalDays + 28; 
      } else if (i == 1 || i == 3 || i == 5 ||i == 7 || i == 8 || i == 10 || i == 12) { 
       totalDays = totalDays + 31; 
      } 
       totalDays = totalDays + 30; 
      } 
     } else if (initialMonth > endMonth) { //if the birthday is earlier than the current month, 
      for (int i = endMonth; i <= initialMonth - 1; i++) { //adds total days until birthday until month of birthday is reached 
      if (i == 2) { 
       totalDays = totalDays + 28; 
      } else if (i == 1 || i == 3 || i == 5 ||i == 7 || i == 8 || i == 10 || i == 12) { 
       totalDays = totalDays + 31; 
      } 
       totalDays = totalDays + 30; 
      } 
      totalDays = 365 - totalDays; 
     } 
     return totalDays; 
    } 

    public static void finalReport(int daysUntilEndOfMonth, int totalDaysOne, int totalDaysTwo, int birthDayOne, int birthDayTwo) { 
     int untilOne = daysUntilEndOfMonth + totalDaysOne + birthDayOne; //calculates days until first person's birthday 
     int untilTwo = daysUntilEndOfMonth + totalDaysTwo + birthDayTwo; //calculates days until second person's birthday 
     System.out.print("There are " + untilOne + " days until person 1's birthday\n"); //reports number of days until birthday 1 
     System.out.print("There are " + untilTwo + " days until person 2's birthday\n"); //reports number of days until birthday 1 
     if (untilOne < untilTwo) { //figures out which birthday is sooner 
      System.out.println("Person 1's birthday is sooner."); 
     } else { 
      System.out.println("Person 2's birthday is sooner."); 
     } 
    } 
} 
+1

에 대한 다른 블록을 넣어하는 것을 잊지. 가능한 한 쉽게 귀하를 도와 드리겠습니다. –

+1

@HovercraftFullOfEels 당신이 무엇을 요구하는지 조심하십시오.) – MadProgrammer

+3

나는 당신에게 한 마디 [JodaTime] (http://joda-time.sourceforge.net/)를 가지고 있습니다. 전체 코드를 줄이면 4 행 – MadProgrammer

답변

0
if (initialMonth < endMonth) { 
    for (int i = initialMonth; i < endMonth; i++) { // adds total days until birthday until month of birthday is reached 
     if (i == 2) { 
      totalDays = totalDays + 28; 
     } else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { 
      totalDays = totalDays + 31; 
     } else { 
      totalDays = totalDays + 30; 
     } 
    } 
} else if (initialMonth > endMonth) { // if the birthday is earlier than the current month, 
    for (int i = endMonth; i <= initialMonth - 1; i++) { // adds total days until birthday until month of birthday is reached 
     if (i == 2) { 
      totalDays = totalDays + 28; 
     } else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { 
      totalDays = totalDays + 31; 
     } else { 
      totalDays = totalDays + 30; 
     } 
    } 
    totalDays = 365 - totalDays; 
} 

이 사이트에 귀하의 질문에 여기에 코드를 게시하시기 바랍니다 totalDays = totalDays + 30;