Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JCreator上的Java(使用while循环创建闰年程序) public class第二年{ 公共静态void main(字符串[]args){ 国际年=1900; 而这正是你想要做的:_Java_While Loop_Multiple Conditions - Fatal编程技术网

JCreator上的Java(使用while循环创建闰年程序) public class第二年{ 公共静态void main(字符串[]args){ 国际年=1900; 而这正是你想要做的:

JCreator上的Java(使用while循环创建闰年程序) public class第二年{ 公共静态void main(字符串[]args){ 国际年=1900; 而这正是你想要做的:,java,while-loop,multiple-conditions,Java,While Loop,Multiple Conditions,公共课测试年{ public class LeapYear_2 { public static void main(String[] args) { int year = 1900; while (year <= 2100 && (year % 4 == 0)){ System.out.println(year + " Is a Leap Year"); y

公共课测试年{

public class LeapYear_2 {

    public static void main(String[] args) {

    int year = 1900;                
        while (year <= 2100 && (year % 4 == 0)){

            System.out.println(year + " Is a Leap Year");
            year++;

            System.out.println(year + " Is not a leap year");
            year++;
        }   

    }
}
可以转化为:当条件为真时,我将doSomething()当它不再为真时,我将继续


在您的原始代码中,条件是
年我相信这就是您要寻找的:

 if year is divisible by 400 then
   is_leap_year
 else if year is divisible by 100 then
   not_leap_year
 else if year is divisible by 4 then
   is_leap_year
 else
   not_leap_year
publicstaticvoidmain(字符串[]args){
国际年=1900;

而(年只是为了好玩一个作弊代码;)

int count=0;
对于(int i=1900;i<2100;i++){
如果(计数=4){
System.out.println(i+“是闰年”);
计数=0;
}
计数++;
}

此单一条件将根据您的需要工作 这里我使用了三元运算符,它的工作原理与if-else条件相同, 基本语法是
您的条件?true:false

int count = 0;
for (int i = 1900; i < 2100; i++){
 if (count == 4){
  System.out.println(i + " Is a Leap Year");
  count = 0;
  }
  count++;
}
在您的情况下
条件:第%4年==0
正确:一年是闰年
错:一年不是闰年
国际年=1900;

虽然(year我想补充一点,在大多数给出的答案中,1900年将显示为闰年。这是因为我们只需要检查年份是否可以被4整除,但百年也应该被400整除。因此,1900年不是闰年,而2000年是

实际情况应该是这样的

in your case 
    condition : year % 4==0 
    true :  year Is a Leap Year
    false : year Is a not Leap Year


            int year = 1900;                
            while (year <= 2100){
                System.out.println(year % 4==0 ? year+" Is a Leap Year" :year + " Is not a leap year" );
                year++;
            }
此外,当我在做这件事时,你也可以做以下事情

if(year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
  // now we have a leap year.

但是,与模运算相比,重复200次要花很多钱。

谢谢,这正是我想做的……现在我知道我可以将if-else语句放入while循环中……非常感谢!另一个问题,先生!我有没有办法将闰年和非闰年分组……是的,你可以看到我的编辑。不是吗对于初学者来说,这是最好的方法,但知道它的存在和它的作用是很好的does@JoeyEguna你也可以把一段时间放进一个IF中。实际上,你可以尽你所能地去深思熟虑。考虑一下投票,接受对你有用的答案。先生,该怎么做?(DoIFeTe: DoiffalSE)你说的可读性差是什么意思?谢谢你,先生!这真的很有帮助!你还应该解释什么是三元运算符以及它的作用,对于初学者来说这并不容易。我想补充一点,在大多数给出的答案中,1900将显示为闰年。这是因为只有一个检查,看看年份是否可以被4整除,但是一个世纪一年也应该被400整除。因此,1900年不是闰年,而2000年是。@kmae是的,你是对的,我编辑了我的答案以考虑到这一点。
public static void main(String[] args) {
int year = 1900;                
    while (year <= 2100){
        if (year % 4 == 0) {
            System.out.println(year + " Is a Leap Year");
            year++;
        }
        else {
            System.out.println(year + " Is not a leap year");
            year++;
        }
    }
}
int count = 0;
for (int i = 1900; i < 2100; i++){
 if (count == 4){
  System.out.println(i + " Is a Leap Year");
  count = 0;
  }
  count++;
}
in your case 
    condition : year % 4==0 
    true :  year Is a Leap Year
    false : year Is a not Leap Year


            int year = 1900;                
            while (year <= 2100){
                System.out.println(year % 4==0 ? year+" Is a Leap Year" :year + " Is not a leap year" );
                year++;
            }
if(year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
  // now we have a leap year.
Calendar cal = Calendar.getInstance();   
cal.set(Calendar.YEAR, year);   
if(cal.getActualMaximum(DAY_OF_YEAR) > 365)
  // leap year.