次の割り当てに問題があります。
「スペースで区切られた月日年 (8 23 2000) の形式で任意の 2 つの日付を受け入れ、開始日と終了日を含めて 2 つの日付の間に経過した合計日数を計算するプログラムを作成してください。覚えておいてください。その閏年は 4 で割り切れる年ですが、1600 年や 2000 年 (1800 年は閏年ではありません) のように 400 で割り切れなければならない 100 周年を除きます。ただし、最終的には以下に示すデータで実行する必要があります。画面上のシステム出力は許容されます。"
これまでのところ、このコードがあり、コンパイルされます。
import java.util.Scanner;
import java.io.*;
public class Project3
{
public static void main(String[] args) throws IOException
{
int m1, d1, y1, m2, d2, y2;
Scanner scan = new Scanner(new FileReader("dates.txt"));
for(int i = 0 ; i < 6; ++i)
{
m1 = scan.nextInt();
d1 = scan.nextInt();
y1 = scan.nextInt();
m2 = scan.nextInt();
d2 = scan.nextInt();
y2 = scan.nextInt();
System.out.println("The total number of days between the dates you entered are: " + x(m1, m2, d1, d2, y1, y2));
}
}
public static int x (int m1, int d1, int y1, int m2, int d2, int y2)
{
int total = 0;
int total1 = 0;
int total2 = 0;
if( m1 == m2 && y1 == y2)
{
total = d2 - d1 +1;
}
else if ( y1 == y2 )
{
total = daysInMonth( m2 , y2 ) - d1 + 1;
}
for(int i = m1; i < m2 ; ++i)
{
total1 += daysInMonth( m2, y2 );
}
for (int i = y1; i < y2; i++)
{
total2 += daysInYear ( y2 );
}
total += total1 + total2;
return total;
}
//Methods
public static boolean isLeap(int yr)
{
if(yr % 400 == 0)
return true;
else if (yr % 4 == 0 && yr % 100 !=0)
return true;
else return false;
}
public static int daysInMonth( int month , int year)
{
int leapMonth;
if (isLeap(year) == true)
{
leapMonth = 29;
}
else
{
leapMonth = 28;
}
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 4:
case 6:
case 9:
case 11: return 30;
case 2: return leapMonth;
}
return 28;
}
public static int daysInYear(int year)
{
if (isLeap(year))
return 366;
else
return 365;
}
}
私が抱えている問題は、データ ファイルの日付の出力が正しくないことです。この 2 つの日付を入力する7 4 1776
と、正解の代わりに1 1 1987
と が得られます。入力した他の日付も正しくありません。723795
76882
これは私が得るエラーでもあります。
入力した日付間の合計日数は次のとおりです: 723795
スレッド「メイン」での例外 java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner. java:1485)
で java.util.Scanner.nextInt(Scanner.java:2117)
で java.util.Scanner.nextInt(Scanner.java:2076)
で Project3.main(Project3.java:15) で
データファイル:
7
4
1776
1
1
1987
どうぞ、どんな助けでも大歓迎です!