您的位置:首页 > 编程语言 > Java开发

PTA_时间换算_JAVA

2019-04-30 19:45 1491 查看

时间换算

本题要求编写程序,以

hh:mm:ss
的格式输出某给定时间再过n秒后的时间值(超过23:59:59就从0点开始计时)。

输入格式:

输入在第一行中以

hh:mm:ss
的格式给出起始时间,第二行给出整秒数n(<60)。

输出格式:

输出在一行中给出

hh:mm:ss
格式的结果时间。

输入样例:

11:59:40
30

输出样例:

12:00:10

import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int hours , minutes , seconds , temp,addition , all_seconds;
String all , temp_string;
all = input.next();
temp_string = all.substring(all.lastIndexOf(":")+1);
seconds = Integer.parseInt(temp_string);
temp_string = all.substring(0, all.indexOf(":"));
hours = Integer.parseInt(temp_string);
temp_string = all.substring(all.indexOf(":")+1, all.lastIndexOf(":"));
minutes = Integer.parseInt(temp_string);
addition = input.nextInt();
all_seconds = minutes * 60 + hours*3600 + seconds;
all_seconds = all_seconds +addition;
hours = all_seconds/3600;
temp = all_seconds;
temp = temp%3600;
if(hours>=24)
hours = hours%24;
minutes = temp / 60;
seconds = temp % 60;
System.out.printf("%02d:%02d:%02d", hours,minutes,seconds);
input.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: