计算小时加1

时间:2013-11-11 20:53:44

标签: java

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

2 个答案:

答案 0 :(得分:1)

首先,您没有使用nexthr

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

其次,你可以在这里找到很多东西。只需hourValueminuteValue即可跟踪输入的时间。将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);