学习 Java 我当前的问题是下面的“else”语句。如果我输入 3 小时 55 分钟,我要查找的结果是:“五比四”
使用下面的代码我得到: 请输入营业时间(1 点到 12 点之间): 3 请输入分钟(1 到 60 之间): 55 五比三
有人可以告诉我我做错了什么吗? (谢谢)
// actual calcuations
if(minuteValue==0){
result = hourPart + minutePart + " o'clock"; // eg two o'clock (the String o'clock defined later in the minute values)
}
else if (minuteValue <= 30 ){
result = minutePart + " past " + hourPart; // eg ten past five
}
else
{
int remainingtime = 60 - minuteValue;
int nexthr = hourValue + 1;
if(nexthr==13) nexthr = 1;
result = getHourString(remainingtime) + " to " + hourPart; // eg twenty to five
}
return result;
最佳答案
首先,您没有使用nexthr
:
result = getHourString(remainingtime) + " to " + hourPart; //where's nexthr?
其次,这里发生了很多事情。只需使用 hourValue
和 minuteValue
来跟踪输入的时间。将 minutePart
更改为 monthValue
,将 hourPart
更改为 hourValue
即可完成:
String result;
int minuteValue = 55;
int hourValue = 9;
if(minuteValue==0){
result = hourValue + minuteValue + " o'clock"; // eg two o'clock (the String o'clock defined later in the minute values)
}
else if (minuteValue <= 30 ){
result = minuteValue + " past " + hourValue; // eg ten past five
}
else
{
int remainingtime = 60 - minuteValue;
int nexthr = hourValue + 1;
if(nexthr==13) nexthr = 1;
result = remainingtime + " to " + nexthr; // eg twenty to five
}
System.out.println(result);
结果:5 到 10
关于java - 计算小时加一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915716/