gpt4 book ai didi

java - if 语句查询问题,eclipse 说 "dead code"

转载 作者:行者123 更新时间:2023-12-03 22:52:31 25 4
gpt4 key购买 nike

首先,非常感谢人们可以提供的任何帮助!

好的,所以我这里有这个循环,而且我似乎对 if 语句有问题。

Eclipse 告诉我下面的查询部分和“else”中的代码是“死代码”:

|| calDateOfDay.DAY_OF_WEEK == Calendar.SUNDAY 

但我不知道为什么。

此外,当它执行时,它总是执行“if”语句中的代码,这(据我所知)意味着每一天都是星期六。但是我知道一个事实,即日期具有正确的值(肯定会检查不是星期六的日期)。我知道这一点,因为我通过 Eclipse 中的代码进行调试,逐日检查日期的值。

老实说,我什至试图解释它都感到困惑! :S

我希望代码做的是:

  1. 取它得到的每一天(一个月的值(value),因月份而异)并检查这一天是星期六还是星期日。
  2. 如果是星期六或星期日,那么我希望它将一天的“类型”设置为周末,如果不是,我希望它将一天的“类型”设置为工作日。

基本上就是这样。

        for (DayEntry dayEntry : daySet){

//day value to be filled
Day day = new Day();
Calendar calDateOfDay = new GregorianCalendar();
//set the date of the day
calDateOfDay.setTime(dayEntry.getDateOfDay());
day.setDate(calDateOfDay);

//set the hours of the day
day.setWorkHours(dayEntry.getHours());

if (Calendar.DAY_OF_WEEK == Calendar.SATURDAY || Calendar.DAY_OF_WEEK == Calendar.SUNDAY ){

day.setType(DayType.WEEK_END);

}else{

day.setType(DayType.WEEK_DAY);

}


//add the day to the week
week.addDay(day);

}

现在,我之所以以上述方式编写代码是因为当我将其编写为如下所示时,Eclipse 告诉我应该以静态方式访问静态变量 DAY_OF_WEEK,所以我也不确定这是否会导致问题。

        for (DayEntry dayEntry : daySet){

//day value to be filled
Day day = new Day();
Calendar calDateOfDay = new GregorianCalendar();
//set the date of the day
calDateOfDay.setTime(dayEntry.getDateOfDay());
day.setDate(calDateOfDay);

//set the hours of the day
day.setWorkHours(dayEntry.getHours());

if (calDateOfDay.DAY_OF_WEEK == Calendar.SATURDAY || calDateOfDay.DAY_OF_WEEK == Calendar.SUNDAY ){

day.setType(DayType.WEEK_END);

}else{

day.setType(DayType.WEEK_DAY);

}


//add the day to the week
week.addDay(day);

}

最佳答案

DAY_OF_WEEK是一个常量,包含星期几的字段编号。您的代码使用它时就好像它包含该字段的,但事实并非如此。

要修复代码,只需将 calDateOfDay.DAY_OF_WEEK 替换为 calDateOfDay.get(Calendar.DAY_OF_WEEK)

关于java - if 语句查询问题,eclipse 说 "dead code",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com