秒数转时间
输入一个秒数将其转换成时间
例:输入3605,打印出01:00:05
效果如图:
代码如下:
package com.jg445.exercise;
import java.util.Scanner;
public class Deta {
public static void main(String[] args) {
Scanner getNum=new Scanner(System.in);
System.out.println("请输入查询的秒数");
int num=getNum.nextInt();
/**小于1小时*/
int minet=num/60;
int second=num%60;
/**大于1小时*/
int hour=num/3600;
int minet2=num%3600/60;
int second2=num%3600%60;
/**小于1小时*/
if(num>=60&&num<3600) {
if(minet>=10) {
if(second>=10) {
System.out.println("当前是北京时间:"+"00:"+minet+":"+second);
}else {
System.out.println("当前是北京时间:"+"00:"+minet+":"+"0"+second);
}
}else {
if(second>=10) {
System.out.println("当前是北京时间:"+"00:"+0+minet+":"+second);
}else {
System.out.println("当前是北京时间:"+"00:"+"0"+minet+":"+"0"+second);
}
}
}else if(num<60){
if(second>=10) {
System.out.println("当前是北京时间:"+"00:"+"00:"+":"+second);
}else {
System.out.println("当前是北京时间:"+"00:"+"00"+":"+"0"+second);
}
} /**大于1小时*/
else {
// 小时是否大于10
if(hour>=10) {
if(minet2>=10) {
if(second2>=10) {
System.out.println("当前是北京时间:"+hour+":"+minet2+":"+second2);
}else {
System.out.println("当前是北京时间:"+hour+":"+minet2+":"+"0"+second2);
}
}else {
if(second>=10) {
System.out.println("当前是北京时间:"+hour+":"+0+minet2+":"+second2);
}else {
System.out.println("当前是北京时间:"+hour+":"+"0"+minet2+":"+"0"+second2);
}
}
}else {
if(minet2>=10) {
if(second2>=10) {
System.out.println("当前是北京时间:"+0+hour+":"+minet2+":"+second2);
}else {
System.out.println("当前是北京时间:"+0+hour+":"+minet2+":"+"0"+second2);
}
}else {
if(second2>=10) {
System.out.println("当前是北京时间:"+0+hour+":"+0+minet2+":"+second2);
}else {
System.out.println("当前是北京时间:"+0+hour+":"+"0"+minet2+":"+"0"+second2);
}
}
}
}
}
}
知识要点:拼接和流程判断