给定秒数seconds ,把秒转化成小时分钟和秒

给定秒数seconds (0< seconds < 100,000,000),把秒转化成小时、分钟和秒
我的代码

public class Main
{
    public static void main(String[] args)
    {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        int s = sc.nextInt();
        int h=0,m=0;
        while(s>=60)
        {
            m++;
            s=s-60;
            while(m>=60)
            {
                h++;
                m=m-60;
            }
			
		}
        System.out.println(h+" "+m+" "+s);
    }
}

别人的代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args)   {
      Scanner sc=new Scanner(System.in);
            int ss=sc.nextInt();
            int h=ss/(60*60);
            int m=(ss/60)-(h*60);
            int s=ss-(h*60*60)-(m*60);
            System.out.println(h+" "+m+" "+s);
    }
}

我的还是比较冗长,慢慢弄

发布了10 篇原创文章 · 获赞 0 · 访问量 76

猜你喜欢

转载自blog.csdn.net/weixin_46265590/article/details/104870858