转换为秒进行比较后,将输出转换回小时分钟秒

时间:2015-09-23 19:05:11

标签: java

我已经运行的代码,它几乎到了我想要的地方,但是我希望system.out以小时分钟和秒显示fastestrunnertime而不是几秒钟,但是我仍然需要保留我的变量s将所有时间转换为秒,以便轻松地比较数据。只是不太确定如何在不中断我的代码的情况下将输出恢复到小时分钟。

   double fastesttime = 1000000;
   int fastestrunnernumber = 1;
   double slowesttime = -1000000;
   int slowestrunnernumber = -1;



    while (reader.hasNext() ) {

    int runner = reader.nextInt();
    int hours = reader.nextInt();
    int minutes = reader.nextInt();
    double seconds = reader.nextDouble();

    if (minutes < 60 && seconds < 60)  {


        }


    else {
        System.out.println("Runner with invalid time: " + runner );



    }
       double s = ((hours * 3600) + (minutes * 60) + (seconds));

       if (s < fastesttime) {
           fastesttime = s;
           fastestrunnernumber = runner;



       }

       if (s > slowesttime) {
           slowesttime = s;
           slowestrunnernumber = runner;


       }



    }






   System.out.println("The fastest runner is " + fastestrunnernumber + " with the time " + fastesttime );

   System.out.println("The slowest runner is " + slowestrunnernumber + " with the time " + slowesttime );

   }

}

}

1 个答案:

答案 0 :(得分:0)

您有两种选择:将原始值保存在新变量中(faster_hour,faster_minute等)或最后执行除法,如下所示:

int hours=fastesttime/3600;
int rest=fastesttime%3600;
int minutes=rest/60;
int seconds=rest%60;

如果你看,你会发现这些是从转换为秒的相反操作。

我希望它有所帮助。