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");
}