寻找下一个leap年 [英] Finding next leapyear

查看:89
本文介绍了寻找下一个leap年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码适用于当年是a年的情况,但是当不是is年时,我需要代码来
找到下一个leap年并打印.我现在拥有的while循环将继续计算年份,例如:2002、2003、2004,并将其打印出来.我可以用什么来对下一年的while循环进行计数,然后再次进行评估过程,直到下一个leap年.我可以在其中放些直到声明吗?谢谢大家


The code works for when the year is a leap year, but when it isnt i need the code to
find the next leap year and print it. The while loop i have now will keep counting the years eg:2002,2003,2004 an prints them. What can i use to have the while loop count the next year and put it through the evaluation process again until it hits the next leapyear. Can i put some sort of until statement in there? Thanks heaps guys


 public static void main(String[] args) {

 int leapyear = 2001;
 boolean isleapyear;

 if (((leapyear % 4 == 0 && leapyear % 100 != 0||leapyear % 400 ==0)))
{
    isleapyear = true;
 }
 else
 {
     isleapyear = false;
 }
 if(isleapyear = true)
 {
    System.out.println(leapyear+" is a leapyear");
 }
 else
 {
     while(isleapyear=false)
     {
        leapyear = leapyear++;
         System.out.println(leapyear+ "is the next leapyear");
     }
 }
}

推荐答案

您需要在main函数旁边创建一个函数,然后从while调用它.
该功能应进行the年检查.

如果您只想查找下一个,请使用已经推荐的break打破while循环-如果要查找所有下一个,则不要这样做(我仍然将其限制为某个maxValue,因为代码会否则,请永远运行.)
You need to create a function beside of the main-function and call it from the while.
The function should do the leap year check.

Use the already recommended break to break up the while loop if you only want to find the next one - don''t do so if you want to find all (I would still limit that to a certain maxValue, cause the code would run forever otherwise).


使用break;在while循环中找到下一个year年的关键字.

谢谢,
Ambesha
Use break; keyword when it found next leap year in your while loop.

thanks,
Ambesha


 if(isleapyear = true)

while(isleapyear=false)


在这两个语句中,缺少=的标志.另外,您的while循环中不应包含System.out.println()语句,否则它可能会声称2003是leap年.
[edit]
实际上,即使将=校正为==,它也无济于事.
[/edit]


A bit short of = signs in these two statements. Also your while loop should not contain the System.out.println() statement, otherwise it may claim that 2003 is a leap year.

[edit]
Actually that while loop is pointless, even after correcting the = to == it will not work.
[/edit]


这篇关于寻找下一个leap年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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