java习题时间类

java习题时间类_第1张图片
class MyTime
{
	private int hour;
	private int minute;
	private int second;
	public MyTime(){};
	public MyTime(int hour,int minute,int second)
	{
		this.hour=hour;
		this.minute=minute;
		this.second=second;
	}
	public void judge()
	{
	if(second>=60)
	while(second>=60)
	{
		this.minute++;
		this.second-=60;
	}
	else
	while(second<0)
	{
		this.minute--;
		this.second=60+second;
	}
	if(minute>=60)
	while(minute>=60)
	{
		this.hour++;
		this.minute-=60;
	}
	else
	while(minute<0)
	{
		this.hour--;
		this.minute+=60;
	}
	this.hour=(hour+24)%24;
	}
	
	public void addsecond(int sec)
	{
	second+=sec;
	this.judge();
	}
	public void addminte(int min)
	{
	minute+=min;
	this.judge();
	}
	public void addhour(int hou)
	{
		hour+=hou;
		hour%=24;
	}
	public void subsecond(int sec)
	{
		second-=sec;
		this.judge();
	}
	public void subminute(int min)
	{
		minute-=min;
		this.judge();
	}
	public void subhour(int hou)
	{
		hour-=hou;
		hour+=24;
		hour%=24;
	}
	public void outing()
	{
	System.out.println(this.hour+"时"+this.minute+"分"+this.second+"秒");
	}
}

public class xiti3
{
	public static void main(String[] args)
	{
	MyTime times=new MyTime(39,59,10);
	times.addsecond(50);
	times.outing();
	}
}
	
	

你可能感兴趣的:(java)