while循环中的多个条件 [英] Multiple conditions in a while loop

查看:169
本文介绍了while循环中的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CountingSundays {

    public static void main(String args[]) {

        Calendar cal = new GregorianCalendar(1901, 00, 01); // month set to 0for jan , 1= feb etc

        while((cal.get(Calendar.YEAR) != 2001) && (cal.get(Calendar.MONTH) != 0) && cal.get(Calendar.DAY_OF_MONTH) != 1) { // while not 1/1/2001

                System.out.print(cal.get(Calendar.MONTH));
            // cal.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}

我试图遍历while循环通过一次添加一天,但它甚至不会第一次访问while循环。 while循环中的条件是否正确?当我测试它时,它只使用一个条件,但当我添加第二个条件时停止。

Im trying to iterate through the while loop by adding on a day at a time but it wont even access the while loop the first time. Are the conditions in the while loop right? When i tested it it worked with just one condition but stopped when i added the second condition.

推荐答案

应该是

while( !(cal.get(Calendar.YEAR) == 2001 && cal.get(Calendar.MONTH) == 0 && cal.get(Calendar.DAY_OF_MONTH) == 1) ) { // while not 1/1/2001

这篇关于while循环中的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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