好的,我正在尝试制作一个用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.");
}
}
由于
EDIT 目前输出如下:
1 2 3 ... 有&#34; x&#34;闰年在这个范围内。
我希望输出看起来像这样:
有&#34; x&#34;闰年在这个范围内。
1 2 3 ...
答案 0 :(得分: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);
}
}
或者,如果由于某种原因你只想要打印ONCE,因为你的问题有点不清楚,你可以在开始之前将println()移动到循环外部,但每次都会打印0。 / p>
答案 1 :(得分: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