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

查看:79
本文介绍了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);
        }
    }
}

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.

解决方案

It should be

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天全站免登陆