0

我正在我的 Java 课上做家庭作业。我得到了大部分内容,除了关于经过时间的部分。我们必须使用方法。这是我的作业和代码。

“您刚刚被一家公司雇用来完成每周的工资单。您每天必须执行的一项功能是检查员工考勤卡并计算他们“打卡”和“打卡”之间经过的时间。你有时还必须将小时转换为分钟,将天转换为小时,将分钟转换为小时,将小时转换为天。由于您刚刚完成了第一堂编程课程,因此您决定编写一个程序来帮助您完成工作。

您决定按以下方式构建您的程序。主要功能只是一个菜单,用户可以从中选择以获取他们想要的信息。菜单上的每个选项都会调用特定的方法来解决任务和/或输出答案。

对于这个程序,您可以假设所有经过的时间都在一天之内,但其他时间可能会更远。请务必提供足够的测试数据来证明您的解决方案是正确的。(为每个转换显示至少一个输出[对于选项#5 可能有几个])。"

import java.util.*;
public class Prog671A
{
    public static void hoursToMinutes()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter hour(s): ");
        int hours = input.nextInt();
        System.out.println(hours + " * 60 = " + (hours * 60) + " minutes.");
        System.out.println("");
    }
    
    public static void daysToHours()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter day(s): ");
        int days = input.nextInt();
        System.out.println(days + " * 24 = " + (days * 24) + " hours.");
        System.out.println("");
    }
    public static void minutesToHours()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter minute(s): ");
        int minutes = input.nextInt();
        System.out.println(minutes + " / 60 = " + ((double)minutes / 60) + " hours.");
        System.out.println("");
    }
    public static void hoursToDays()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter hour(s): ");
        int hours = input.nextInt();
        System.out.println(hours + " / 24 = " + ((double)hours / 24) + " days.");
        System.out.println("");
    }
    public static void elapsedTime()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the beginning hour: ");
        int startingHour = input.nextInt();
        System.out.print("Enter the beginning minute(s): ");
        int startingMinutes = input.nextInt();
        System.out.print("Enter AM/PM: ");
        String startingAmOrPm = input.nextLine();
        System.out.print("Enter the ending hour: ");
        int endingHour = input.nextInt();
        System.out.print("Enter the ending minute(s): ");
        int endingMinutes = input.nextInt();
        System.out.print("Enter AM/PM: ");
        String endingAmOrPm = input.nextLine();
        System.out.println("");
        System.out.println("The elapsed time is: " + );
    }
    public static void main (String args [])
    {
        int x = 1;
        while (x == 1) {
        Scanner input = new Scanner (System.in);
        System.out.println("Conversion Tasks");
        System.out.println("\t1. Hours -> Minutes");
        System.out.println("\t2. Days -> Hours");
        System.out.println("\t3. Minutes -> Hours");
        System.out.println("\t4. Hours -> Days");
        System.out.println("\t5. Elapsed time between two times");
        System.out.println("\t6. Exit");
        System.out.print("Enter a number: ");
        int menu = input.nextInt();
        System.out.println("");
        if (menu == 1)
            hoursToMinutes();
        if (menu == 2)
            daysToHours();
        if (menu == 3)
            minutesToHours();
        if (menu == 4)
            hoursToDays();
        if (menu == 5)
            elapsedTime();
        if (menu == 6)
            x = 0;
        }
    }
}

我只是在这里需要帮助

public static void elapsedTime()
{
    Scanner input = new Scanner (System.in);
    System.out.print("Enter the beginning hour: ");
    int startingHour = input.nextInt();
    System.out.print("Enter the beginning minute(s): ");
    int startingMinutes = input.nextInt();
    System.out.print("Enter AM/PM: ");
    String startingAmOrPm = input.nextLine();
    System.out.print("Enter the ending hour: ");
    int endingHour = input.nextInt();
    System.out.print("Enter the ending minute(s): ");
    int endingMinutes = input.nextInt();
    System.out.print("Enter AM/PM: ");
    String endingAmOrPm = input.nextLine();
    System.out.println("");
    System.out.println("The elapsed time is: " + );
}
4

2 回答 2

3

只需将开始和结束时间转换为自一天开始(午夜)以来的分钟数。对于下午,只需在转换为分钟之前将 12 添加到小时。然后减去并转换回经过时间的小时和分钟。

于 2012-12-26T21:15:29.977 回答
-2

你为什么要手动花费时间等东西?读取用户登录时的系统日期 System.currentTimeMillis() 将此时间存储在某处

当用户存在时,同样获取系统时间。从用户登录时最初存储的时间中减去它。

我希望这就是你要找的东西?

于 2012-12-26T21:12:50.883 回答