2013-09-30 41 views
0

我被困在這個問題上。Java - 天計算程序

編寫一個程序,輸入兩個日期(僅限月份和日期)並顯示兩個日期之間有多少天。假定這兩個日期都在2013年之內。程序必須驗證輸入的每個日期以檢查該日是否在1和該月的最大天數之間。使用數組來存儲每月的最大天數並輸出經過的天數。

由於與同事/熟人複製我的代碼的經驗,刪除了代碼。

問題是每當I輸入January 1作爲第一日期和April 1作爲第二日期,它只輸出31天而真正的答案是90天。誰能幫忙?編輯: 它現在正常工作。感謝那些幫助過的人。

回答

0

你的邏輯是有缺陷的。你需要做這樣的事情: -

int start = 0; 
int middle = 0; 
int end = 0; 
int sum = 0; 
... 
if (code2 > code) { 
    start = Days[code] - inputDay + 1; 
    for (int count = inputMonth; count < code2; count++) { 
     middle += Days[count]; 
    } 
    end = inputDay2; 
} else { 
    start = inputDay2 - inputDay + 1; 
} 

sum = start + middle + end; 
2

變化

for (int count = inputMonth; count > inputMonth2; count++)

for (int count = inputMonth; count < inputMonth2; count++)

+0

現在的工作,但是當我輸入2月20日3月1日輸出爲12天。如果2013年不是閏年,那麼這個答案將起作用 – Nyaou

+0

這不是一個完整的答案。代碼中還有其他邏輯錯誤。 – Ron

+0

已經算出來了。感謝您的幫助。 – Nyaou

0

使用java.util.Calendar

int diff = 0; 

Calendar begin = Calendar.getInstance(); 
begin.set(Calendar.MONTH, beginMonth); 
begin.set(Calendar.DAY_OF_MONTH, beginDay); 

Calendar end = Calendar.getInstance(); 
end.set(Calendar.MONTH, endMonth); 
end.set(Calendar.DAY_OF_MONTH, endDay); 

while(begin.compareTo(end) <= 0) { 
    diff++; 
    begin.add(Calendar.DAY_OF_MONTH, 1); 
} 

System.out.println(diff); 

這樣API本身就會處理好幾個月的天數。此外,這更靈活,因爲即使給定的日期不在同一年(您需要代碼中的小改動),您也可以找到差異。

+0

不能使用,因爲我只能使用數組 – Nyaou

0

你在for循環使用的條件>當它應該是<

for (int count = inputMonth; count < inputMonth2; count++) { 
    middle = middle + Days[count]; 
} 

你的代碼中包含的邏輯錯誤的大量(例如:什麼當同月2個日期用於發生),下面是一個工作版本:

public static void main(String[] args) { 
    String monthStr; 
    String dayStr; 
    int inputMonth; 
    int inputDay; 
    String monthStr2; 
    String dayStr2; 
    int inputMonth2; 
    int inputDay2; 
    int code; 
    int code2; 
    int start; 
    int middle = 0; 
    int end; 
    int sum; 

    String[] Months = { "January", "February", "March", "April", "May", 
      "June", "July", "August", "September", "October", "November", 
      "December" }; 

    int[] Days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

    monthStr = JOptionPane.showInputDialog(null, "Enter First Month: "); 
    inputMonth = Integer.parseInt(monthStr); 

    dayStr = JOptionPane.showInputDialog(null, "Enter First Day: "); 
    inputDay = Integer.parseInt(dayStr); 

    monthStr2 = JOptionPane.showInputDialog(null, "Enter Second Month: "); 
    inputMonth2 = Integer.parseInt(monthStr2); 

    dayStr2 = JOptionPane.showInputDialog(null, "Enter Second Day: "); 
    inputDay2 = Integer.parseInt(dayStr2); 

    if (inputMonth == inputMonth2) { 
     // Same months 
     sum = inputDay2 - inputDay; 
    } else { 
     // Different months 
     start = Days[inputMonth - 1] - inputDay; 
     end = inputDay2; 
     middle = 0; 

     // Start at inputMonth+1, start already included the days remaining 
     // in inputMonth 
     for (int count = inputMonth + 1; count < inputMonth2; count++) { 
      middle = middle + Days[count - 1]; 
     } 
     sum = start + middle + end; 
    } 
    JOptionPane.showMessageDialog(null, "DAYS COMPUTATION PROGRAM" 
      + "\nFirst Date: " + Months[inputMonth - 1] + " " + inputDay 
      + "\nSecond Date: " + Months[inputMonth2 - 1] + " " + inputDay2 
      + "\nNumber of Days Elapsed: " + sum + " Days"); 
}