有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何让Java应用程序读取用户时间输入,然后进行转换和计算?

enter image description here

^这就是我想要效仿的

是的,我最近正试图学习Java,我在这个练习中感到迷失了方向

我做了一些搜索,以找到要使用的代码片段,我也在使用教科书:大Java Late对象

我希望编写一个Java应用程序,用户在其中输入滑雪运动员的完成时间(用于一场名为Vasaloppet的比赛)。所有参与者的开始时间假定为08:00,这是硬编码的

该应用程序将显示滑雪者从塞伦到莫拉的时间,然后以小时、分和秒为单位显示

完成《瓦萨洛佩特》的最快时间是2012年,当时约根·布林克以03:38:41的成绩越过终点线。该项目现在将展示今天的滑雪者有多慢(或快)

我想把时间转换成秒,用时间(秒)减去记录时间(秒)。然后将结果转换回要显示的小时、分钟和秒

这是我目前的代码:

import java.util.Scanner;   /// makes the scanner class available

public class Vasalopp {

public static void main(String[] args) {


    System.out.println("Vasaloppet - Input your finish time");  /// headline
    System.out.println("------------------------------------------------------------------");   /// for stylish purpose

    Scanner input = new Scanner(System.in); /// scanner object to make the application able to read keyboard input
    System.out.print("Hours: ");
    String inputHours = input.nextLine(); ///user promted to input number of hours
    System.out.print("Minutes: "); 
    String inputMinutes = input.nextLine(); ///user promted to input number of minutes
    System.out.print("Seconds: "); 
    String inputSeconds = input.nextLine(); ///user promted to input number of seconds

    /// constants
    int SECONDS_IN_AN_HOUR = 3600;
    int MINUTES_IN_AN_HOUR = 60;
    int SECONDS_IN_A_MINUTE = 60;
    int STARTING_TIME = 28800;
    int RECORD_TIME = 13121;


    System.out.print("Your time is: \t");
    System.out.println(); 
    System.out.println("------------------------------------------------------------------"); 

    System.out.print("Record time 2012 were: \t"); 
    System.out.println();
    System.out.println("------------------------------------------------------------------"); 

    System.out.print("The time differences is: \t");
    System.out.println("------------------------------------------------------------------"); 


}
}

我基本上有两个问题无法解决:

  1. 在这种情况下,我如何让用户能够输入完成时间 案例14:10:55
  2. 有谁能给我一个关于如何解决时间转换的提示吗

我知道数学必须是这样的,例如:

记录时间为03:38:41,可以写成

  • 13121秒13121/3600=3.64472222<;-3小时
  • (3.64472222-3)*60=38.68333333<;-38分钟
  • (38.68333-38)*60=41秒

我知道模数%,但不知道如何正确使用它

基本上,我不知道如何将其解释为Java代码。我尝试了几种方法,但似乎没有成功

非常感谢


共 (1) 个答案

  1. # 1 楼答案

    如果可以的话,我推荐Joda,例如

    Period p = new Period(RECORD_TIME * 1000L);
    System.out.println(p.getHours());
    System.out.println(p.getMinutes());
    System.out.println(p.getSeconds())
    

    否则,您可以手动进行计算

    int hours = RECORD_TIME / SECONDS_IN_AN_HOUR;
    int minutes = (RECORD_TIME - SECONDS_IN_AN_HOUR * hours) / MINUTES_IN_AN_HOUR;
    int seconds = RECORD_TIME % SECONDS_IN_A_MINUTE;