1

学习 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;
4

2 回答 2

1

那么首先你没有使用nexthr

result = getHourString(remainingtime) + " to " + hourPart; //where's nexthr?

其次,你有很多事情发生在这里。只需拥有hourValueminuteValue跟踪输入的时间。更改minutePartminuteValuehourParthourValue您就完成了:

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 to 10

于 2013-11-11T21:09:03.343 回答
0
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);
于 2013-11-14T21:12:24.277 回答