java - 从午夜开始的时、分、秒方法和表格中的输出
问题描述
有人可以在我的 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 个问题:
- 我的 getTimeSinceMidnight 数学是否正确?
- 我是否需要使用枚举数字类型,或者我分配的变量是否适合我?
- 我如何将我的输出变成我上面需要的相应输出?
谢谢!!
解决方案
推荐阅读
- octave - 绘制 f(x,y,z) 消失的 3D 曲面
- javascript - 在 Twilio 中获取两个数字之间的消息
- reactjs - 无法在带有状态的 React 模式中编辑文本以编辑任务标题和正文
- c++ - 如何为向量向量释放内存(C++)
- here-api - Here-API 找不到 normal.day.yaml
- java - 在 Java 中使用 HashMap 计算平均(等级)
- python - 如何在实际数据图的顶部绘制预测?
- discord.py - 当机器人进入服务器并且检测到不存在具有通道名称的预先存在的服务器时,如何创建文本通道
- python - Python:如何匹配字符串列表中的特定单词,无论它是由大写字母还是小写字母组成
- snowflake-cloud-data-platform - 有没有办法减少雪管加载时间?