仍在研究这个愚蠢的时间计划....我的新问题在于计算,分钟似乎并没有增加总数,我不确定为什么...任何输入输出将一如既往地受到高度赞赏: )
public class Time {
public int Minutes, Hours, TotalMin;
private String correctedMinutes ;
public String valid, TimeofDay;
public String error, noerror;
//-----------------------------------------------------------------
// Sets up the time by defining its minutes and hours
//-----------------------------------------------------------------
public Time() {
this.Hours = 0;
this.Minutes = 0;}
public Time(int hour, int minute, String timeofday) {
this.Hours = hour;
this.Minutes = minute;
this.TimeofDay = timeofday;}
public String testTimeofDay ( String timeofday)
{
if (TimeofDay.equals ("AM")) {TimeofDay="AM";
return "";}
else if (TimeofDay.equals("PM")) {TimeofDay="PM";
return "";}
else {TimeofDay="AM";
return "Invalid Entry: AM/PM";}
}
public String testHoursMinutes(int hours, int minutes)
{
if (Minutes>59|| Minutes<0||Hours>23 || Hours<0)
{Minutes=0;
Hours=0;
TimeofDay="AM";
return "Invalid Entry: Incorrect Value";}
else {Hours=hours;
Minutes=minutes;
return "";}
}
public int addMinutes (int hours, int minutes, int elapsedMin)
{
if (elapsedMin<0)
TotalMin=minutes;
else {
TotalMin=elapsedMin+Minutes;}
return TotalMin;}
public String ConvertMintoHour (int hours, int minutes){
while (TotalMin>60)
{TotalMin=TotalMin-60;
Hours=Hours+1;
if (Hours==13)
{ Hours=0;
Minutes=TotalMin;
}}
return(Hours+ " " +Minutes);
}
public String toString ()
{
String correctedMinutes = String.format("%02d", Minutes);
return (Hours+ ":" + correctedMinutes + TimeofDay); }
}
这是测试代码:
import java.util.Scanner;
public class TimeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int minutes, hours, elapsedMin;
String timeofday, startTime;
Scanner scan=new Scanner (System.in);
System.out.println("Please enter starting hour (1-12):");
hours=scan.nextInt();
System.out.println("Please enter starting minute (0-59):");
minutes=scan.nextInt();
System.out.println("Please enter AM or PM:");
timeofday=scan.next();
Time classtime = new Time (hours, minutes, timeofday);
System.out.println(classtime.testTimeofDay(timeofday));
System.out.println(classtime.testHoursMinutes (hours, minutes));
System.out.println(classtime.toString());
System.out.print("The class begins at ");
System.out.println (classtime.toString());
startTime=classtime.toString();
System.out.println("Enter a time in minutes representing the class duration:");
elapsedMin=scan.nextInt();
classtime.addMinutes (hours, minutes, elapsedMin);
System.out.println ("Class End Time:" + classtime.toString());
System.out.print(elapsedMin + " minutes after "+ startTime + " is ");
System.out.println (classtime.toString());
System.out.print("The class ends at ");
System.out.println (classtime.toString());
System.out.print("The class ends at ");
System.out.print("MILITARY");
}}