用Java创建Time类

时间:2015-02-03 06:02:35

标签: java time

所以我的任务是创建一个Time类,它包含一个小时值和一个分钟值来表示时间。我将使用“军事时间”,因此上午12:01是0001而下午1点是1300.我可以假设有效的军事时间范围从0000到2359.有效标准时间范围从凌晨12:00到晚上11:59。时间应包括两个构造函数:

Time() - 将时间设置为1200的默认构造函数。 时间(int h,int m) - 如果h介于1和23之间,则将小时设置为h。否则,将小时设置为0.如果m介于0和59之间,则将分钟设置为m。否则,将分钟设置为0。 时间应包括以下方法:

String toString() - 以格式:0819返回长度为4的字符串的时间。请注意,如果小时或分钟是一位数,则应首先打印零。例如,6应打印为06。

String convert() - 返回从军事时间转换为标准时间的字符串的时间。例如,0545变为早上5:45,而1306变为1:06 pm。

void increment() - 将时间提前一分钟。请记住,60分钟= 1小时。因此,如果您的时间是0359,并且您添加一分钟,它将变为0400. 2359应该增加到0000。

这是我写的(主要方法除外):

import java.io.IOException;

public class Time
{

    private int hour;
    private int minute;
    public Time()
    {
        hour = 12;
        minute = 0;
    }


    public Time(int h, int m)
    {
        if ( h >= 1 && h <= 23)
            h = hour;
        else 
            hour = 0;
        if ( m >= 0 && m <= 59)
            m = minute;
        else 
            minute = 0;
    }

    public String toString()
    {
        String s = "";
        if ( hour < 10 && minute < 10)
            s = "0" + hour + "0" + minute;
        else if ( hour < 10 && minute > 10)
            s = "0" + hour + minute;
        else if ( hour > 10 && minute < 10)
            s = hour + "0" + minute;
        else if ( hour == 0)
            s = "0" + hour + minute;
        else if ( minute == 0)
            s = hour + "0" + minute;

        return s;

    }

    public String convert()
    {
        String c = "";
        if ( hour > 12)
            c = "0" + (24 - hour) + minute + "pm";
        else
            c = hour + minute + "am";
        return c;
    }

    public void increment()
    {
        minute++;
        if (minute == 60)
        {
            hour++;
            minute = 0;
        }
        else if ( hour == 24)
            hour = 0;

    }

     public static void main(String str[]) throws IOException
     {

      Time time1 = new Time(14, 56);
      System.out.println("time1: " + time1);
      System.out.println("convert time1 to standard time: " + time1.convert());
      System.out.println("time1: " + time1);
      System.out.print("increment time1 five times: ");
      time1.increment();
      time1.increment();
      time1.increment();
      time1.increment();
      time1.increment();
      System.out.println(time1 + "\n");

      Time time2 = new Time(-7, 12);
      System.out.println("time2: " + time2);
      System.out.print("increment time2 67 times: ");
      for (int i = 0; i < 67; i++)
       time2.increment();
      System.out.println(time2);
      System.out.println("convert to time2 standard time: " + time2.convert());
      System.out.println("time2: " + time2 + "\n");


      Time time3 = new Time(5, 17);
      System.out.println("time3: " + time3);
      System.out.print("convert time3: ");
      System.out.println(time3.convert());

      Time time4 = new Time(12, 15);
      System.out.println("\ntime4: " + time4);
      System.out.println("convert time4: " + time4.convert());

      Time time5 = new Time(0, 15);
      System.out.println("\ntime5: " + time5);
      System.out.println("convert time5: " + time5.convert());

      Time time6 = new Time(24, 15);
      System.out.println("\ntime6: " + time6);
      System.out.println("convert time6: " + time6.convert());

      Time time7 = new Time(23,59);
      System.out.println("\ntime7: " + time7);
      System.out.println("convert time7: " + time7.convert());
      time7.increment();
      System.out.println("increment time7: " + time7);
      System.out.println("convert time7: " + time7.convert());

     }

}

输出应如下:

time1: 1456
convert time1 to standard time: 2:56 pm
time1: 1456
increment time1 five times: 1501

time2: 0012
increment time2 67 times: 0119
convert to time2 standard time: 1:19 am
time2: 0119

time3: 0517
convert time3: 5:17 am

time4: 1215
convert time4: 12:15 pm

time5: 0015
convert time5: 12:15 am

time6: 0015
convert time6: 12:15 am

time7: 2359
convert time7: 11:59 pm
increment time7: 0000
convert time7: 12:00 am

我目前的输出是:

time1: 0000
convert time1 to standard time: 0am
time1: 0000
increment time1 five times: 0005

time2: 0000
increment time2 67 times: 0107
convert to time2 standard time: 8am
time2: 0107

time3: 0000
convert time3: 0am

time4: 0000
convert time4: 0am

time5: 0000
convert time5: 0am

time6: 0000
convert time6: 0am

time7: 0000
convert time7: 0am
increment time7: 0001
convert time7: 1am

我的编译器告诉我全部都有问题,我无法弄清楚在哪里。如果有人可以请一看,帮我弄清楚什么是错的,这会非常有帮助。谢谢!

5 个答案:

答案 0 :(得分:1)

构造函数的问题,你没有将值赋给类变量而是分配给方法变量,这就是你的代码的样子,

public Time(int h, int m)
    {
        if ( h >= 1 && h <= 23)
            h = hour;
        else 
            hour = 0;
        if ( m >= 0 && m <= 59)
            m = minute;
        else 
            minute = 0;
    }

改为使用下面的Time构造函数,

public Time(int h, int m)
        {
            if ( h >= 1 && h <= 23)
                hour =h;
            else
                hour = 0;
            if ( m >= 0 && m <= 59)
                minute = m;
            else
                minute = 0;
        }

答案 1 :(得分:1)

如前面的答案所述,构造函数的赋值是相反的。

同样是您的toString方法,在其中添加最终else子句     s = hour + minute;

答案 2 :(得分:1)

您的问题出在构造函数,toString() convert()方法中。

相应的代码更改是:

public MultiLevel(int h, int m)
{
    if ( h >= 1 && h <= 23)
        hour = h;
    else 
        hour = 0;
    if ( m >= 0 && m <= 59)
        minute = m;
    else 
        minute = 0;
}

public String toString()
{
    String s = "";
    if ( hour < 10 && minute < 10)
        s = "0" + hour + "0" + minute;
    else if ( hour < 10 && minute > 10)
        s = "0" + hour + minute;
    else if ( hour > 10 && minute < 10)
        s = hour + "0" + minute;
    else if ( hour > 10 && minute > 10)
        s = hour  +""+ minute;
    else if ( hour == 0)
        s = "0" + hour + minute;
    else if ( minute == 0)
        s = hour + "0" + minute;

    return s;

}

public String convert()
{
    String c = "";

    if ( hour > 12)
    {
        if(hour >10){
            if((hour - 12) == 12)
                 c = (hour - 12) +":"+ minute + "am";
            else
               c = (hour - 12) +":"+ minute + "pm";
        }
        else
        {
            c = "0" + (hour - 12) +":"+ minute + "pm";
        }
    }
    else
        c = hour +":"+ minute + "am";
    if(minute == 0)
    {
        c= c.substring(0, c.length()-2)+"0"+c.substring(c.length()-2, c.length());
    }
    return c;
}

答案 3 :(得分:0)

import java.io.IOException;

public class Time
{

    private int hour;
    private int minute;
    public Time()
    {
        hour = 0;
        minute = 0;
    }


    public Time(int h, int m)
    {
        if ( h >= 1 && h <= 23)
            hour =h;
        else
            hour = 0;
        if ( m >= 0 && m <= 59)
            minute = m;
        else
            minute = 0;
    }



    public String toString()
    {
        String s = "";
        if ( hour < 10 && minute < 10)
            s = "0" + hour + "0" + minute;
        else if ( hour < 10 && minute > 10)
            s = "0" + hour + minute;
        else if ( hour > 10 && minute < 10)
            s = hour + "0" + minute;
        else if ( hour > 10 && minute > 10)
            s = hour +""+ minute;
        else if ( hour == 0)
            s = "0" + hour + minute;
        else if ( minute == 0)
            s = hour + "0" + minute;

        return s;

    }

    public String convert()
    {
        String c = "";

        if (hour >11)
        {
            if (hour == 12)
            {
                c = hour +":"+ minute + " PM";
            }
            if (hour > 12)
            {
                c = (hour - 12) +":"+ minute + " PM";
            }
        }
        else if (hour == 0)
        {
            c = (hour+12) +":"+ minute + " AM";
        }

        else
             c = hour +":"+ minute + " AM";











        if(minute < 10)
        {
            c= c.substring(0, c.length()-4)+"0"+minute+" "+c.substring(c.length()-2, c.length());
        }
        return c;
    }
    public void increment()
    {
        if (hour == 23 && minute == 59)
        {
            hour = 0;
            minute = 0;
        }
        else
        {
        minute++;
        if (minute == 60)
        {
            hour++;
            minute = 0;
        }
        else if ( hour == 24)
            hour = 0;
        }
    }

     public static void main(String str[]) throws IOException
     {

         Time time1 = new Time(14, 56);
          System.out.println("time1: " + time1);
          System.out.println("convert time1 to standard time: " + time1.convert());
          System.out.println("time1: " + time1);
          System.out.print("increment time1 five times: ");
          time1.increment();
          time1.increment();
          time1.increment();
          time1.increment();
          time1.increment();
          System.out.println(time1 + "\n");

          Time time2 = new Time(-7, 12);
          System.out.println("time2: " + time2);
          System.out.print("increment time2 67 times: ");
          for (int i = 0; i < 67; i++)
           time2.increment();
          System.out.println(time2);
          System.out.println("convert to time2 standard time: " + time2.convert());
          System.out.println("time2: " + time2 + "\n");


          Time time3 = new Time(5, 1);
          System.out.println("time3: " + time3);
          System.out.print("convert time3: ");
          System.out.println(time3.convert());

          Time time4 = new Time(12, 15);
          System.out.println("\ntime4: " + time4);
          System.out.println("convert time4: " + time4.convert());

          Time time5 = new Time(0, 15);
          System.out.println("\ntime5: " + time5);
          System.out.println("convert time5: " + time5.convert());

          Time time6 = new Time(24, 15);
          System.out.println("\ntime6: " + time6);
          System.out.println("convert time6: " + time6.convert());

          Time time7 = new Time(23,59);
          System.out.println("\ntime7: " + time7);
          System.out.println("convert time7: " + time7.convert());
          time7.increment();
          System.out.println("increment time7: " + time7);
          System.out.println("convert time7: " + time7.convert());

     }

}

你走了。我有同样的任务,这给了我100%。我清理了很多你的代码,这是一大堆意大利面条。您遇到的最大错误是您在开始时将小时初始化为12而不是零。你在这里和那里有一些错误,我忘了注意,但这将解决你的问题!

答案 4 :(得分:0)

我已经从上述代码中进行了一些改进

  

添加异常类和字符串生成器,并在代码中建议要改进的新事物

异常代码在下面

 [{
    "current_speed": "19.09",
    "_east": "-87.654561",
    "_last_updt": "2018-07-17 15:50:54.0",
    "_region_id": "1",
    "_north": "42.026444",
    "_south": "41.997946",
    "region": "Rogers Park - West Ridge",
    "_west": "-87.709645",
    "_description": "North of Devon. Kedzie to Lake Shore"
}, {
    "current_speed": "25.23",
    "_east": "-87.747456",
    "_last_updt": "2018-07-17 15:50:54.0",
    "_region_id": "2",
    "_north": "42.0190998",
    "_south": "41.960669",
    "region": "Far North West",
    "_west": "-87.84621",
    "_description": "North of Montrose. East River to Cicero"
}

即兴代码

  public class InvalidTimeException extends Exception {

    public InvalidTimeException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public InvalidTimeException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
        super(arg0, arg1, arg2, arg3);
        // TODO Auto-generated constructor stub
    }

    public InvalidTimeException(String arg0, Throwable arg1) {
        super(arg0, arg1);
        // TODO Auto-generated constructor stub
    }

    public InvalidTimeException(String arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public InvalidTimeException(Throwable arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }





}