首页 > 解决方案 > 从午夜开始的时、分、秒方法和表格中的输出

问题描述

有人可以在我的 getTimeSinceMidnight 方法上检查我的数学,看看它是否会返回自午夜以来的小时、分钟和秒。我将展示一个我希望输出如何的示例。我的输出有一个 TimeOfDay.java 和一个 test.java。此外,在标题/方向中,它要求 getTimeSinceMidnight 具有枚举的数值类型,我不知道如何获得这些。

这是我的代码:

//Write class for TimeOfDay
//Instace Data for: Mon, Tue, Wed, Thu, Fri, Sat, Sun
//Data for: hours, minutes, seconds, AM or PM
//Create appropriate getter and setter methods for each instace data
//Write method called getTimeSinceMidnight to return number of hours or seconds
//since midnight
//This method should accept enumerated data types:HOURS, MINUTES, SECONDS
//should return an int value

import java.text.*;
import java.util.*;

public class TimeOfDay
{
  //declare variables
  private int hours, minutes, seconds, SSM, MSM, HSM;
  private String ampm;

  //TimeOfDay constructor

  public TimeOfDay(int h, int m, int s, String ap)
  {
    hours = h;
    minutes = m;
    seconds = s;
    ampm = ap;
      if (ampm.equals("PM"))
      {
        h = hours + 12;
      }
      else
      {
         ampm.equals("AM");
      }
  }

  //Setter Methods for instance data

  public void setDay(String d) {day = d;}
  public void setHours(int h) {hours = h;}
  public void setMinutes(int m) {minutes = m;}
  public void setSeconds(int s) {seconds = s;}
  public void setAmPm(String AmPm) {ampm = AmPm;}

  //Getter Methods for instance data

  public String getDay() {return day;}
  public double getHours() {return hours;}
  public int getMinutes() {return minutes;}
  public int getSeconds() {return seconds;}
  public String getAmPm() {return ampm;}

  //Method to return the HOURS, MINUTES, SECONDS since midnight

  public int getTimeSinceMidnight()
  {
    SSM = hours * 3600;
    return SSM;
    MSM = minutes + hours * 60;
    return MSM;
    HSM = hours + (minutes * 60) + (seconds/60) * 60;
    return HSM;
  }
  //toString Method

  public String toString()
  {
    String s;
    DecimalFormat df = new DecimalFormat("0#");
    s = df.format(hours) + ":" + df.format(minutes);

    s = String.format("%1$2s", hours) + ":";
    return s;
  }
}

这是我的测试程序:

//Test program c
//output the amount of time since midnight
//output as hours, minutes, seconds
//hours output should be in format of ##.### (use double variables for this)
//minutes and seconds just output as integers
//use only integers for your variables involved in minutes and seconds calculations
//Read in (no prompts) a time of day, end when user types QUIT or quit
//Time of day will look like: 4:15:00 PM



import java.util.*;
import java.text.*;
import java.text.DecimalFormat;

public class testTimeC
{
  public static void main(String[] args)
  {
    int hours, minutes, seconds;
    String ampm;

    Scanner s = new Scanner(System.in);
    s.useDelimiter(":|\\s+"); // helps the scanner know when numbers end - :

    while (s.hasNextLine())
    {
      hours = s.nextInt();
      minutes = s.nextInt();
      seconds = s.nextInt();
      ampm = s.nextLine().trim(); // remove leading spaces from " AM" or " PM"
      System.out.println("hours = " + hours);
      System.out.println("minutes = " + minutes);
      System.out.println("seconds = " + seconds);
      System.out.println(ampm);
      //add the remaining printlns if you wish to see the rest of the data
    }
  }
}

测试程序的输出应该是这样的:

对应输出

 TIME          HOURS   MINUTES    SECONDS
 4:15:00 PM   16.250       975      58500 
11:59:00 PM   23.983      1439      86340 
 8:00:10 AM    8.000       480      28810 
12:45:20 PM   12.750       765      45920 
 2:37:00 AM    2.617       157       9420

因此,将其简化为 2-3 个问题:

  1. 我的 getTimeSinceMidnight 数学是否正确?
  2. 我是否需要使用枚举数字类型,或者我分配的变量是否适合我?
  3. 我如何将我的输出变成我上面需要的相应输出?

谢谢!!

标签: java

解决方案


推荐阅读