知道为什么这不起作用?下面的实际代码和调试器的运行时间图,如果语句应该设置变量Testnumber = 1;但是当它被评估时它是= 0 ?????
private String Convert2Time(int Number){
int Hours, Minutes, HrsRemainder, MinRemainder, Seconds; String RetString, OneString, TwoString, ThreeString, HoursStr, MinStr, SecStr;
byte Testnumber = 0;
Hours = Number / 3600;
HrsRemainder = Number % 3600;
Minutes = HrsRemainder / 60;
MinRemainder = HrsRemainder % 60;
Seconds = MinRemainder / 1;
HoursStr = Integer.toString(Hours);
MinStr = Integer.toString(Minutes);
SecStr = Integer.toString(Seconds);
ThreeString = HoursStr + " Hrs " + MinStr + " Min " + SecStr + " Sec";
TwoString = MinStr + " Min " + SecStr + " Sec";
OneString = SecStr + " Sec";
if (Hours < 0){
Testnumber = 2;
} else {
if (Minutes < 0){
Testnumber = 1;
}
}
switch (Testnumber){
case 2:
RetString = ThreeString;
break;
case 1:
RetString = TwoString;
break;
default:
RetString = OneString;
}
return RetString;
}
答案 0 :(得分:3)
根据您的输出,Hours < 0
和Minutes < 0
都评估为false
。没有理由设置TestNumber = 1;
甚至TestNumber = 2;
。它距离0
byte Testnumber = 0;
答案 1 :(得分:0)
在阅读完您的方法后,您似乎希望将秒数转换为可读时间。
主要问题是条件。小时,分钟,秒将始终具有有效的更大或等于零,它们不会是负数。因为你正在划分正数。
因此private float RoundToNearestEven(float input){
// if no decimal type make it even
if(input % 1 == 0)
{
if(input % 2 != 0)
{
// arbitrary add 1
return input + 1;
}
// already even
return input;
}
bool isPositive = (input > 0);
float absInput = Mathf.abs(input)
float roundedInput = Mathf.Round(absInput);
if(isPositive && roundedInput % 2 == 0)
{
return roundedInput;
}
else
{
return -roundedInput;
}
// was rounded up
if(isPositive && input % 1 > .5)
{
return roundedInput - 1;
}
else if (isPositive && input % 1 < .5)
{
//was rounded down
return roundedInput + 1;
}
else if (input % 1 > .5)
{
//negative and was rounded up
return -(roundedInput - 1);
}
else
{
return -(roundedInut + 1);
}
, private int round_to_even(float input){return Mathf.Round(input / 2.0) * 2; }
等任何条件都不会返回true;
你需要反转它们。因为只有在有任何小时,分钟,秒时,您才希望在响应中添加正确的块。
Hours < 0
答案 2 :(得分:0)
因为您不必要地使代码复杂化。不要重新发明轮子。使用现有的Java类为您完成工作:
int numInSeconds = 61;
int millis = numInSeconds * 1000;
Date time = new Date(millis);
int hours = time.getHours();
int mins = time.getMinutes();
int secs = time.getSeconds();
StringBuilder builder = new StringBuilder();
if (hours > 0) {
builder.append(hours);
builder.append(" hrs ");
}
// etc.
String displayTime = builder.toString(); // "1 mins 1 secs"