我对编程很陌生,这段代码还远未完成,所以请原谅业余爱好者。
此代码是通过使用多个类将时间添加到运行时间
这是我的时间课程:
public class Time {
private int Minutes, Hours, TotalMin;
private String stringAM= "AM", stringPM = "PM", correctedMinutes ;
public String valid, TimeofDay;
public static int error;
public Object toString;
//-----------------------------------------------------------------
// Sets up the time by defining its minutes and hours
//-----------------------------------------------------------------
public Time(int hours, int minutes)
{ if (minutes>59|| minutes<0|| hours>23 || hours<0) {
Hours=0;
Minutes=0;
error=1;
return;
}
else {Hours=hours;
Minutes=minutes;
error=0;
return;
}}
public int Time2(int minutes, int hours) {
if (Hours<12 || Hours>0 || TimeofDay.equals ("AM") || TimeofDay.equals ("PM")) {
Hours=0;
Minutes=0;
return error=0;
}
else {
Hours=hours;
Minutes=minutes;
return error=0;
}
}
public int addMinutes (int Hours, int Minutes, int elapsedMin)
{
if (elapsedMin<0)
return error=1;
else {
TotalMin=elapsedMin+Minutes;
return error=0;}
}{
while (TotalMin>60)
{TotalMin=TotalMin-60;
Hours=Hours+1; if (Hours==25)
{ Hours=0;
Minutes=TotalMin;
}
}
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;
String TimeofDay;
Scanner scan=new Scanner (System.in);
System.out.println("Please enter starting hour:");
hours=scan.nextInt();
System.out.println("Please enter starting minute:");
minutes=scan.nextInt();
System.out.println("Please enter AM or PM:");
TimeofDay=scan.next();
Time classtime = new Time (hours, minutes);
{}
classtime.Time2(minutes, hours);
int error=Time.error;
if (error==1)
System.out.println("This is an invalid time");
System.out.println (classtime.toString);
System.out.print("The class ends at ");
System.out.println (classtime.toString);
}}
基本上我只是想不通为什么当我运行我的代码时它没有完成运行而是打印:
"Null
The class ends at Null"
有什么见解吗?我意识到代码还有其他问题,但是一旦我可以让它运行更多,我会修复这些问题
(顺便说一句,我知道 Time 2 方法似乎非常多余,我的老师在课堂上需要它,否则我会将它包含在构造函数中)
任何意见将不胜感激