我正在创建一种方法,我必须通过使用简单的 // 语句来决定我在一天中的else
哪个小时else if
。else
我不明白为什么程序会直接进入该else
部分。
我的逻辑有什么问题?我知道这个问题很值得问,但我很困惑,我真的很想知道这里出了什么问题。
public String whichClass()
{
String string = "";
int ClassLength = 2;
this.setLengthOfOS(ClassLength);
this.setLengthOfSecurity(ClassLength);
this.setLengthOfForensics(ClassLength);
this.setOSStartHour(13);
this.setSecurityStartHour(15);
this.setForensicsStartHour(17);
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
this.setHour(c.get(Calendar.HOUR_OF_DAY));
if ((this.getHour() > this.getOSStartHour()) &&
(this.getHour() < this.getSecurityStartHour()))
{
string = "We're in OS class.";
}
else if ((this.getHour() > this.getSecurityStartHour()) &&
(this.getHour() < this.getForensicsStartHour()))
{
string = "We're in Security class.";
}
else
{
string = "We have no class.";
}
return string;
}
编辑:这是纠正和工作。
public String whichClass()
{
String string = "";
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
this.setHour(c.get(Calendar.HOUR_OF_DAY));
if ((this.getHour() >= this.OSStartHour) && (this.getHour() < (this.OSStartHour + this.lengthOfOS))) string = "We're in OS class.";
else if ((this.getHour() >= this.SecurityStartHour) && (this.getHour() < (this.SecurityStartHour + this.lengthOfSecurity))) string = "We're in Security class.";
else if ((this.getHour() >= this.ForensicsStartHour) && (this.getHour() < (this.ForensicsStartHour + this.lengthOfForensics))) string = "We're in Security class.";
else string = "We have no class.";
return string;
}