仍然工作在這種愚蠢的時間程序....在計算中,似乎分鐘再予我的新問題是不被加總,我不知道爲什麼...任何INOUT將不勝感激一如既往:)計算錯誤
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");
}}
如果你知道錯誤是你清點最後幾分鐘,那麼只發布那麼一小段代碼也許並不是一個壞主意,也許是一個剪枝的版本這就是它的名字。它會幫助你找到問題,並且如果你仍然陷入困境,讓我們更容易。 –
抱歉,我遇到了一些問題,比我預期的要深,所以我覺得把它放在一起並不會讓人受傷。 – user2908551
不,不,發佈這一切通常不是一個好主意,特別是如果它是巨大的。 –