学习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;
答案 0 :(得分:1)
首先,您没有使用nexthr
:
result = getHourString(remainingtime) + " to " + hourPart; //where's nexthr?
其次,你可以在这里找到很多东西。只需hourValue
和minuteValue
即可跟踪输入的时间。将minutePart
更改为minuteValue
,将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 to 10
答案 1 :(得分: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);