输出int但带有小数

时间:2015-12-18 01:22:46

标签: java

所以我有一段代码,我希望将秒输出为整数,但我不想丢失十进制值,因为我需要将它作为for循环中的计数的一部分来编码程序正确,这里是代码(for循环显然未完成,因为没有计数)

         System.out.println("Recommended Splits for Marathon for time of "
 + hours + ":" + minutes + ":" + seconds);

            hoursInSeconds = hours * 3600;
                minutesInSeconds = minutes * 60;
                totalTime = hoursInSeconds + minutesInSeconds + seconds;
                double totalSecondsPerKM = totalTime/42;
                int hoursPerKM =   (int) (totalSecondsPerKM/3600);
                double remainderHours = totalSecondsPerKM%3600;
                int minutesPerKM =  (int) (totalSecondsPerKM/60);
                double secondsPerKM = (totalSecondsPerKM%60);


            for (int index = 1; index <=42; index++){
                System.out.print(index + " ");





                System.out.print("" +hoursPerKM);
                System.out.print("");
                System.out.print(":"+minutesPerKM);
                System.out.print("");
                System.out.print(":"+secondsPerKM);
                System.out.println("");

1 个答案:

答案 0 :(得分:1)

您可以使用

System.out.println(String.format("%.0f", 1.6));

System.out.println((int)1.6);

第一个会围绕值,输出2,第二个会截断它,输出1