所以我有一个程序可以打印出你从用户给定的日期到一年中的天数,其中包括闰年。但是,当我编译程序时,它说变量 dayNumber 未初始化。我该如何解决?
import java.util.Scanner;
public class Days
{
public static void main( String [] args )
{
Scanner scan = new Scanner(System.in);
int month, day, year;
int dayNumber;
System.out.print("Enter the month: ");
month = scan.nextInt();
System.out.print("Enter the day: ");
day = scan.nextInt();
System.out.print("Enter the year: ");
year = scan.nextInt();
if ( month == 1 )
dayNumber = day;
else if ( month == 2 )
dayNumber = 31 + day;
else if ( month == 3 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + day;
else
dayNumber = 31 + 28 + day;
else if ( month == 4 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + day;
else
dayNumber = 31 + 28 + 31 + day;
else if ( month == 5 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + day;
else
dayNumber = 31 + 28 + 31 + 30 + day;
else if ( month == 6 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + day;
else if ( month == 7 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + day;
else if ( month == 8 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
else if ( month == 9 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + 31 + + 31 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
else if ( month == 10 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + 31 + + 31 + 30 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
else if ( month == 11 )
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + 31 + + 31 + 30 + 31 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
else
if ( year % 400 == 0 )
if ( year % 4 == 0 && year % 100 != 0 )
dayNumber = 31 + 29 + 31 + 30 + 31 + 30 + 31 + + 31 + 30 + 31 + 30 + day;
else
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
System.out.println("The date " + month + "/" + day + "/" + year + " is day number " + dayNumber);
}
}
感谢您提供的任何帮助!