VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • 【JAVA】编程(2)---时间管理

作业要求:

定义一个名为 MyTime 的类,其中私有属性包括天数,时,分,秒;定义一个可以初始化时,分,秒的构造方法,并对初始化数值加以限定,以防出现bug;定义一个方法,可以把第几天,时,分,秒打印出来;通过定义方法可以分别对时,分,秒进行加运算,要保证天数,时,分,秒间的进制转换无误;

调试:在主方法中创建time对象,同时初始化时,分,秒;再对时,分,秒进行加运算,并将时间结果打印出来:

import java.util.*;
public class 时间管理 {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("请依次初始化时间:时:分:秒 (注意若初始化数值不合理,系统将默认初始化其值为0)");
        int inTimeHour=s.nextInt();
        int inTimeMinute=s.nextInt();
        int inTimeSecond=s.nextInt();
        MyTime time=new MyTime(inTimeHour,inTimeMinute,inTimeSecond);
        System.out.println("快进 ? 时:");
        inTimeHour=s.nextInt();
        time.addHour(inTimeHour);
        System.out.println("快进 ? 分:");
        inTimeMinute=s.nextInt();
        time.addMinute(inTimeMinute);
        System.out.println("快进 ? 秒:");
        inTimeSecond=s.nextInt();
        time.addSecond(inTimeSecond);
        time.display();
    }
}
class MyTime {
    protected int hour;
    protected int minute;
    protected int second;
    protected int day;
    public MyTime() {
    }
    public MyTime(int hour, int minute, int second) {
        if(hour>=24){
            setHour(0);
        }
        else this.hour = hour;
        if(minute>=60){
            setMinute(0);
        }
        else this.minute = minute;
        if(second>=60){
            setSecond(0);
        }
        else this.second = second;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public int getHour() {
        return hour;
    }
    public void setHour(int hour) {
        this.hour = hour;
    }
    public int getMinute() {
        return minute;
    }
    public void setMinute(int minute) {
        this.minute = minute;
    }
    public int getSecond() {
        return second;
    }
    public void setSecond(int second) {
        this.second = second;
    }
    public void display() {
        System.out.println("目前时空:"+"第"+getDay()+"天:"+getHour() + ":" + getMinute() + ":" + getSecond());
    }
    public void addSecond(int second) {
        int sec = this.second + second;
        if (sec >= 60) {
            addMinute((int) (sec / 60));
            setSecond(sec % 60);
        } else {
            setSecond(sec);
        }
    }
    public void addMinute(int minute) {
        int min = this.minute + minute;
        if (min >= 60) {
            addHour((int) (min / 60));
            setMinute(min % 60);
        } else {
            setMinute(min);
        }
    }
    public void addHour(int hour) {
        int hou= this.hour + hour;
        if (hou >= 24) {
            addDay((int) (hou / 24));
            setHour(hou % 24);
        } else {
            setHour(hou);
        }
    }
    public void addDay(int day) {
        setDay(day);
    }
}



 

博主能力有限,若程序有bug或有其他不当之处,请狠狠打脸博主           (~ ̄(OO) ̄)ブ

 

来源:https://www.cnblogs.com/Burning-youth/p/15527798.html


相关教程