标签:存储 笔记 font 范围 算法 技术分享 time static rgs
目录
本题主要考查类的相关定义及应用,下面具体代码对于时、分、秒的数据范围处理的部分代码,可以单独写一个函数来处理,这样可以复用代码,但是为了配合题目要求,就直接把代码复制了一遍运用。
具体代码如下:
import java.util.Scanner; public class Main { public int hour; public int minute; public int second; Main(int hour, int minute, int second) { //获取秒 if(second >= 60) { minute += second / 60; second %= 60; this.second = second; } else if(second < 0) { int temp = second / 60; int temp1 = second % 60; if(temp1 == 0) { this.second = 0; minute += temp; } else { this.second = 60 + temp1; minute += temp - 1; } } else { this.second = second; } //获取分钟 if(minute >= 60) { hour += minute / 60; minute %= 60; this.minute = minute; } else if(minute < 0) { int temp = minute / 60; int temp1 = minute % 60; if(temp1 == 0) { this.minute = 0; hour += temp; } else { this.minute = 60 + temp1; hour += temp - 1; } } else { this.minute = minute; } //获取小时 if(hour >= 24) this.hour = hour % 24; else if(hour < 0) this.hour = 24 + hour % 24; else this.hour = hour; } public void adv(int hour, int minute, int second) { second += this.second; //获取要求格式秒 if(second >= 60) { minute += second / 60; second %= 60; this.second = second; } else if(second < 0) { int temp = second / 60; int temp1 = second % 60; if(temp1 == 0) { this.second = 0; minute += temp; } else { this.second = 60 + temp1; minute += temp - 1; } } else { this.second = second; } minute += this.minute; //获取要求格式分钟 if(minute >= 60) { hour += minute / 60; minute %= 60; this.minute = minute; } else if(minute < 0) { int temp = minute / 60; int temp1 = minute % 60; if(temp1 == 0) { this.minute = 0; hour += temp; } else { this.minute = 60 + temp1; hour = hour + temp - 1; } } else { this.minute = minute; } hour += this.hour; //获取要求格式小时 if(hour >= 24) this.hour = hour % 24; else if(hour < 0) this.hour = 24 + hour % 24; else this.hour = hour; } public void reset() { this.hour = 0; this.minute = 0; this.second = 0; } public void print() { if(hour >= 10) System.out.print(hour+":"); else { System.out.print("0"+hour+":"); } if(minute >= 10) System.out.print(minute+":"); else System.out.print("0"+minute+":"); if(second >= 10) System.out.print(second+"\n"); else System.out.print("0"+second+"\n"); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int hour1 = in.nextInt(); int minute1 = in.nextInt(); int second1 = in.nextInt(); int hour2 = in.nextInt(); int minute2 = in.nextInt(); int second2 = in.nextInt(); Main test = new Main(hour1, minute1, second1); test.print(); test.adv(hour2, minute2, second2); test.print(); test.reset(); test.print(); } }
算法笔记_092:蓝桥杯练习 c++_ch04_02_修正版(Java)
标签:存储 笔记 font 范围 算法 技术分享 time static rgs
原文地址:http://www.cnblogs.com/liuzhen1995/p/6574898.html