Java 如何获得每行10年-闰年
请帮助格式化我的输出 我被要求“编写一个程序,显示二十一世纪(从2001年到2100年)的所有闰年,每行十年,用一个空格隔开” 虽然我得到了正确的结果,但它不是要求的格式 提前谢谢Java 如何获得每行10年-闰年,java,Java,请帮助格式化我的输出 我被要求“编写一个程序,显示二十一世纪(从2001年到2100年)的所有闰年,每行十年,用一个空格隔开” 虽然我得到了正确的结果,但它不是要求的格式 提前谢谢 public class Leapyear { public static void main(String[] args) { //declare variabls; int year; int count=1; int yearsperline = 1
public class Leapyear {
public static void main(String[] args) {
//declare variabls;
int year;
int count=1;
int yearsperline = 10;
//loop
for(year=2001;2001<=2100;year++){
if((year%4==0 && year%100!=0) || (year%400==0))
System.out.print(year+",");
if ( year ==2100)
break;
while (count%10==0)
System.out.println();
}
}
}
公共类{
公共静态void main(字符串[]args){
//声明变量;
国际年;
整数计数=1;
int yearsperline=10;
//环路
对于(year=2001;2001你可以这样写:
//declare variables;
final int YEARS_PER_LINE = 10;
final int START_YEAR = 2001;
//loop
for(int year = START_YEAR; year <= 2100; year++){
System.out.print(year);
if((year - START_YEAR) % YEARS_PER_LINE == (YEARS_PER_LINE - 1)) {
System.out.println();
} else {
System.out.print(",");
}
}
//声明变量;
每行最后整数年=10;
最终int开始年份=2001年;
//环路
对于(int year=START_year;year请尝试以下方法:
public class LeapYear
{
public static void main(String[] args)
{
// declare variables
final int startYear = 2001;
final int endYear = 2100;
final int yearsPerLine = 10;
// loop
for (int year = startYear, count = 0; year <= endYear; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
if (count % yearsPerLine != 0)
System.out.print(" ");
else if (count > 0 && count % yearsPerLine == 0)
System.out.println();
System.out.print(year);
++count;
}
}
}
}
公共类
{
公共静态void main(字符串[]args)
{
//声明变量
最终int startYear=2001;
最终int endYear=2100;
最后一个国际年斯珀林=10;
//环路
对于(int year=startYear,count=0;year 0&&count%yearsPerLine==0)
System.out.println();
系统输出打印(年);
++计数;
}
}
}
}
int startFromYear=2001;
int stopAtYear=2100;
整数计数=0;
对于(int i=startFromYear;i100%正确!且易于理解
public class LeapYears
{
public static void main(String[] args)
{
int count = 0;
for(int i = 2001; i <= 2100; i++)
{
if ((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))
{
count++;
//if count is 10 then start a new line.
if(count % 10 == 0)
{
System.out.println(i);
}
//if count is smaller 10 then in the same line
else
{
System.out.print(i + " ");
}
}
}
}
}
公共类
{
公共静态void main(字符串[]args)
{
整数计数=0;
对于(int i=2001;i您当前获得的输出是什么?您能给出一个当前输出与期望输出的示例吗?2001您似乎没有在任何地方增加count
,因此您的条件count%10==0
将永远不会是真的。也许使用“
而不是”,“
可能满足以下条件:“正好由一个空格分隔”。如果年份仅从2001年到2100年,您只需检查year%4==0
,这将是一个无限循环。编辑:不再是了,条件已修复。堆栈溢出时通常需要一些解释。您确定它100%正确吗?绘图扭曲:2100不是闰年。
public class LeapYears
{
public static void main(String[] args)
{
int count = 0;
for(int i = 2001; i <= 2100; i++)
{
if ((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))
{
count++;
//if count is 10 then start a new line.
if(count % 10 == 0)
{
System.out.println(i);
}
//if count is smaller 10 then in the same line
else
{
System.out.print(i + " ");
}
}
}
}
}