2016-02-04 100 views
0

我一直在试图制作一个简单的程序,它将输出两年之间的闰年。我已经制定了循环,每年检查是否是闰年,然后打印出来,但我希望能够在每一行显示10年,而不是仅在一行中显示全部。Java - 在For循环中的十个值之后打印新行

所以这样的:

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 + ", "); 
       } 
      } 
     } 
    } 
} 
+0

跟踪: 的for(int i = firstY;我<= finalY;我++) 并打印一个新行,如果i是由10个 –

回答

0

这是我会做的轨道。只需添加一个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 + ", "); 
      } 

     } 
    } 

} 
} 
+0

非常感谢整除!完美的作品。 – JavaLava

+0

@JavaLava如果这是解决方案,请继续并将其标记为“已接受的答案”,以方便其他人查找。 – Spencer4134

0

使用外部计数器(比如int printed = 0)来跟踪您已经打印了多少个号码。检查你的内循环是否printed % 10 == 0,并在这种情况下请致电System.out.println()

0

您可以使用计数器保持环路

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++; 
      } 

     } 
    } 
1

你可以这样做。你在for循环,其中的

int count = 0; 
for(; startYear <= endYear; startYear++) { 
System.out.print(startYear + ", "); 
count++; 
if(count == 10) { 
    count = 0; 
    System.out.println(); 
} 
}