这是我的代码。我想在早上,下午和晚上进行比较。我可以这样做吗?
private String interpretFT(int morninghour, int afternoonhour, int eveninghour)
{
if (morninghour > afternoonhour )
{
return "Time to exercise : Morning";
}
else if (afternoonhour > eveninghour)
{
return "Time to exercise : Afternoon";
}
else if (eveninghour > morninghour)
{
return "Time to exercise : Evening";
}
else
{
return "Please have the freetime to have some exercise";
}
}
答案 0 :(得分:0)
你想用这个做什么? 如果你想从当前时间获得字符串:
private String interpretFT(int morningHour, int afternoonHour, int eveningHour, int currentHour){
String result = "";
if (currentHour => morninghour && currentHour < afternoonhour )
{
result = "Time to exercise : Morning";
}
if (currentHour => afternoonhour && currentHour < eveninghour)
{
result = "Time to exercise : Afternoon";
}
if (currentHour => eveninghour && currentHour < morninghour)
{
result = "Time to exercise : Evening";
}
else
{
result = "Please have the freetime to have some exercise";
}
return result;
}
答案 1 :(得分:-1)
比较我建议使用三元运算符,例如:
private String interpretFT(int morninghour, int afternoonhour, int eveninghour) {
return ((morninghour > afternoonhour) ? "Time to exercise : Morning" :
(afternoonhour > eveninghour) ? "Time to exercise : Afternoon" :
(eveninghour > morninghour) ? "Time to exercise : Evening" :
"Please have the freetime to have some exercise");
}