Java - 在 For 循环中的十个值后打印新行
Java - Printing new line after ten values in a For loop
我一直在尝试制作一个简单的程序来打印出 2 个输入年份之间的闰年。我做了一个循环,每年检查它是否是闰年,然后打印它,但我希望每行只能显示 10 年,而不仅仅是在一条长线上显示所有年份。
像这样:
1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040,
1044, 1048, 1052, 1056, 1060, 1064, 1068, 1072, 1076, 1080,
1084, 1088, 1092, 1096, 1104, 1108, 1112, 1116, 1120, 1124,
1128, 1132, 1136, 1140, 1144, 1148, 1152, 1156, 1160, 1164,
1168, 1172, 1176, 1180, 1184, 1188, 1192, 1196, 1200.
而不是这个:
1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040, 1044, 1048, 1052...
我尝试了各种不同的方法来实现这一点,嵌套 for 循环等,但目前我似乎无法实现。很抱歉,如果这是一个简单答案的愚蠢问题,我根本没有学习 Java 很长时间。
到目前为止,这是我的代码,它将所有年份打印在一行中:
import java.util.Scanner;
public class LeapYears {
public static void main(String[] args)
{
int firstY, finalY;
Scanner kb = new Scanner(System.in);
System.out.print("Enter start year: ");
firstY = kb.nextInt();
System.out.print("Enter end year: ");
finalY = kb.nextInt();
for(; firstY <= finalY; firstY++) {
if ((firstY % 4) == 0) {
if ((firstY % 100) == 0) {
if ((firstY % 400) == 0 ) {
System.out.print(firstY + ", ");
firstY++;
}
} else {
System.out.print(firstY + ", ");
}
}
}
}
}
使用外部计数器(例如 int printed = 0
)来记录您已经打印了多少个数字。检查你的内部循环是否 printed % 10 == 0
并在这种情况下调用 System.out.println()
。
您可以使用计数器来跟踪循环
int counter =0;
for(; firstY <= finalY; firstY++)
{
if ((firstY % 4) == 0)
{
if ((firstY % 100) == 0)
{
if ((firstY % 400) == 0 )
{
if(counter==10) {
System.out.println("");
}
System.out.print(firstY + ", ");
firstY++;
counter++;
}
}
else
{
System.out.print(firstY + ", ");
counter++;
}
}
}
这就是我会做的。只需添加一个 int 计数器变量,每次打印一年时递增它。当它达到 10 的倍数时,只需打印一个新行。
import java.util.Scanner;
public class LeapYears
{
public static void main(String[] args)
{
int firstY, finalY, counter;
counter = 0;
Scanner kb = new Scanner(System.in);
System.out.print("Enter start year: ");
firstY = kb.nextInt();
System.out.print("Enter end year: ");
finalY = kb.nextInt();
for(; firstY <= finalY; firstY++)
{
if ((firstY % 4) == 0)
{
if ((firstY % 100) == 0)
{
if ((firstY % 400) == 0 )
{
System.out.print(firstY + ", ");
firstY++;
counter++;
if (counter % 10 == 0) System.out.println("");
}
}
else
{
System.out.print(firstY + ", ");
}
}
}
}
}
你可以这样做。
int count = 0;
for(; startYear <= endYear; startYear++) {
System.out.print(startYear + ", ");
count++;
if(count == 10) {
count = 0;
System.out.println();
}
}
我一直在尝试制作一个简单的程序来打印出 2 个输入年份之间的闰年。我做了一个循环,每年检查它是否是闰年,然后打印它,但我希望每行只能显示 10 年,而不仅仅是在一条长线上显示所有年份。
像这样:
1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040,
1044, 1048, 1052, 1056, 1060, 1064, 1068, 1072, 1076, 1080,
1084, 1088, 1092, 1096, 1104, 1108, 1112, 1116, 1120, 1124,
1128, 1132, 1136, 1140, 1144, 1148, 1152, 1156, 1160, 1164,
1168, 1172, 1176, 1180, 1184, 1188, 1192, 1196, 1200.
而不是这个:
1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040, 1044, 1048, 1052...
我尝试了各种不同的方法来实现这一点,嵌套 for 循环等,但目前我似乎无法实现。很抱歉,如果这是一个简单答案的愚蠢问题,我根本没有学习 Java 很长时间。
到目前为止,这是我的代码,它将所有年份打印在一行中:
import java.util.Scanner;
public class LeapYears {
public static void main(String[] args)
{
int firstY, finalY;
Scanner kb = new Scanner(System.in);
System.out.print("Enter start year: ");
firstY = kb.nextInt();
System.out.print("Enter end year: ");
finalY = kb.nextInt();
for(; firstY <= finalY; firstY++) {
if ((firstY % 4) == 0) {
if ((firstY % 100) == 0) {
if ((firstY % 400) == 0 ) {
System.out.print(firstY + ", ");
firstY++;
}
} else {
System.out.print(firstY + ", ");
}
}
}
}
}
使用外部计数器(例如 int printed = 0
)来记录您已经打印了多少个数字。检查你的内部循环是否 printed % 10 == 0
并在这种情况下调用 System.out.println()
。
您可以使用计数器来跟踪循环
int counter =0;
for(; firstY <= finalY; firstY++)
{
if ((firstY % 4) == 0)
{
if ((firstY % 100) == 0)
{
if ((firstY % 400) == 0 )
{
if(counter==10) {
System.out.println("");
}
System.out.print(firstY + ", ");
firstY++;
counter++;
}
}
else
{
System.out.print(firstY + ", ");
counter++;
}
}
}
这就是我会做的。只需添加一个 int 计数器变量,每次打印一年时递增它。当它达到 10 的倍数时,只需打印一个新行。
import java.util.Scanner;
public class LeapYears
{
public static void main(String[] args)
{
int firstY, finalY, counter;
counter = 0;
Scanner kb = new Scanner(System.in);
System.out.print("Enter start year: ");
firstY = kb.nextInt();
System.out.print("Enter end year: ");
finalY = kb.nextInt();
for(; firstY <= finalY; firstY++)
{
if ((firstY % 4) == 0)
{
if ((firstY % 100) == 0)
{
if ((firstY % 400) == 0 )
{
System.out.print(firstY + ", ");
firstY++;
counter++;
if (counter % 10 == 0) System.out.println("");
}
}
else
{
System.out.print(firstY + ", ");
}
}
}
}
}
你可以这样做。
int count = 0;
for(; startYear <= endYear; startYear++) {
System.out.print(startYear + ", ");
count++;
if(count == 10) {
count = 0;
System.out.println();
}
}