0

好的,我正在尝试制作一个用于计算 Java 闰年的程序。该程序的那部分工作正常。程序提示用户 2 年(开始和结束),然后只打印闰年。闰年印刷后,计算印刷了多少。

到目前为止,一切都很好。现在我的问题是如何在实际闰年打印之前打印计数?我相信我必须打印到列表或数组,然后显示计数,然后调用数组/列表在事后打印。但是我相信我让这太复杂了,有没有更简单的方法?请帮忙。

import java.util.Scanner; //Imports Scanner to gather user input.

public class Program
{
    public static void println(Object arg) //Shortens to println to make coding easier to read/follow.
    {
        System.out.println(arg); //prints on new line
    }

    public static void print(Object arg) //Shortens to print to make coding easier to read/follow.
    {
        System.out.print(arg); //prints on current line
    }

    public static void run()
    {
        Scanner in = new Scanner(System.in); //Defines Scanner to gather keyboard input.

        println("Leap Years");
        println("");

        print("Enter the starting year: " );
        while (!in.hasNextInt()) //This loop verifys that the input from the user is numerical and prompts to try again if not.
        {
            print("Numbers only! Enter the starting year: ");
            in.next();
        }
        int y1 = in.nextInt();

        while (y1 <1582) //Leap year was not established until 1582, this while loop ensures no dates before that time.
        {
        println("ERROR: Leap year was not implemented until 1582.");
        print("Enter the starting year: " );
        while (!in.hasNextInt()) 
        {
            print("Numbers only! Enter the starting year: ");
            in.next();
        }
        y1 = in.nextInt();
        }

        print("Enter the ending year: " );
        while (!in.hasNextInt()) 
        {
            print("Numbers only! Enter the ending year: ");
            in.next();
        }
        int y2 = in.nextInt();

        while (y2<y1) //This prevents the user from creating a reverse range.
        {
        println("ERROR: Ending year must not precede starting year.");
        print("Enter the ending year: " );
        while (!in.hasNextInt()) 
        {
            print("Numbers only! Enter the ending year: ");
            in.next();
        }
        y2 = in.nextInt();
        }

        println("");
        println("Leap years according to the Gregorian calendar from "+y1+" to "+y2+":");
        println("");
        int c=0; //Defines the count of leap years
        for(int year=y1; year<=y2; year++) //Prints all years from user range.
        {
           if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) //Prints only leap years from user range.
           {
               ++c;
               println(year);
           }
        }
        println("");
        if(c>1 || c==0)
        println("There are "+c+" leap years in this range.");
        else
        println("There is "+c+" leap year in this range.");
    }
}

谢谢

编辑当前的输出如下所示:

1 2 3 ... 在此范围内有“x”个闰年。

我希望输出看起来像这样:

在此范围内有“x”个闰年。

1 2 3 ...

4

2 回答 2

0

只需将打印操作移动到循环的中间即可打印增量。

for(int year=y1; year<=y2; year++)
    {
       if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
       {
           ++c;
           System.out.println(c);
           println(year);
       }
    }

或者,如果由于某种原因您只希望它打印一次,因为您的问题有些不清楚,您可以在它开始之前将 println() 移动到循环的外部,但它每次都会打印 0。

于 2013-08-28T22:17:33.270 回答
0

您可以将代码更改为这样的:

public static void run()
{
//anything....
ArrayList<Integer> list = new ArrayList<>();
println("");
println("Leap years according to the Gregorian calendar from "+y1+" to "+y2+":");
println("");
for(Integer year=y1; year<=y2; year++) //Prints all years from user range.
{
   if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) //Prints only leap years from user range.
   {
        list.add(year);
   }
}
//Loop and print size() and all leap years here
于 2013-08-28T23:18:40.103 回答