public class Time {
public int Minutes, Hours, TotalMin;
private String correctedMinutes ;
public String valid, TimeofDay;
public static int error;
//-----------------------------------------------------------------
// 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;}
else
{Minutes=minutes;
Hours=hours;
error=0;}
}
public String Time2(int hours, int minutes, String timeofday) {
if (Hours>12 || Hours<0)
return "This is not a valid number of hours";
Hours=0;
if ( minutes>59|| minutes<0)
{Minutes=0;
error=1;
return "This is not a valid number of minutes";}
if (!timeofday.equals ("AM") || !timeofday.equals ("PM"))
{TimeofDay.equals("AM");
return "You incorrectly enetered AM or PM";}
else
{ Hours=hours;
Minutes=minutes;
return "";}
}
//----------------------------------------------------------
//
//
//-----------------------------------------------------------
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); }
测试类
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);
if (Time.error!=0)
System.out.println("This is an invalid time");
System.out.print(classtime.Time2(hours, minutes, timeofday));
{
if (Time.error!=0)
System.out.println("This is an invalid time");
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 (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");
}}}
import java.util.Scanner;
它给了我错误
Exception in thread "main" java.lang.NullPointerException
at Time.Time2(Time.java:39)
at TimeTest.main(TimeTest.java:29)
我知道这不是这段代码唯一的问题,所以如果你有任何建议,我会全力以赴。