Multiple conditions in a while loop

后端 未结 2 555
陌清茗
陌清茗 2021-01-24 22:54
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CountingSundays {

    public static void main(String args[]) {

        Calendar cal = new          


        
相关标签:
2条回答
  • 2021-01-24 23:11

    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
    
    0 讨论(0)
  • 2021-01-24 23:23

    This is just a simple logic error. If even one of those is false (say, if the month IS 0), then you have true && false && true, which is false.

    You need the "not" outside of the entire expression, or you need to use "||" to combine them:

    while( !(year == 2001 && month == 0 && day == 1) )
    

    or

    while( (year != 2011) || (month != 0) || (day != 1) )
    
    0 讨论(0)
提交回复
热议问题