Java-在For循环中的十个值之后打印新行 [英] Java - Printing new line after ten values in a For loop

查看:67
本文介绍了Java-在For循环中的十个值之后打印新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一个简单的程序,以打印出输入的2年之间的the年.我制作了一个循环,每年检查一次,看看是否是and年,然后打印出来,但是我希望每行只能显示10年,而不仅仅是长行显示所有年份.

I have been trying to make a simple program that will print out the leap years between 2 inputted years. I have made the loop that checks each year to see if it is a leap year and then prints it, but I would like to be able to only show 10 years on each line, not just all the of them on one long line.

像这样:

    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... 

我尝试了各种不同的方法来实现此目的,例如嵌套嵌套循环等,但目前看来还无法实现.很抱歉,如果这是一个简单答案的愚蠢问题,我已经很长时间没有学习Java了.

I have tried various different methods to make this happen, nested for loops etc but at the moment I cant seem to make it happen. Sorry if it's a stupid question with a simple answer, I haven't been learning Java for very long at all.

到目前为止,这是我的代码,可以将所有年份打印在一行上:

Here is my code so far which prints all the years on one line:

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计数器变量,就可以在每次打印一年时将其递增.当它达到10的倍数时,只需打印新行.

This is what I would do. Simply add an int counter variable, increment it every time that you print out a year. When it reaches a multiple of 10, simply print a new line.

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

        }
    }

}
}

这篇关于Java-在For循环中的十个值之后打印新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆