A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


* 3、 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:
*键盘输入6,打印6秒;
*键盘输入60,打印1分;
*键盘输入66,打印1分6秒;
*键盘输入666,打印11分6秒;
*键盘输入3601,打印1小时1秒


package com.itheima;

//导包
import java.util.Scanner;

public class Test3 {
        public static void main(String[] args) {
                // 创建一个键盘输入对象
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数字:");
                // 接收键盘输入的值
                int x = sc.nextInt();
                // 分别初始化变量秒、分、小时、天
                int s = 0;
                int m = 0;
                int h = 0;
                int d = 0;
                // 通过对60、24取余分别得出秒、分、小时、天数的值
                if (x / 60 == 0) {
                        s = x;
                }
                if (x / 60 > 0) {
                        m = x / 60;
                        s = x % 60;
                }
                if (m / 60 > 0) {
                        h = m / 60;
                        m %= 60;
                }
                if (h / 24 > 0) {
                        d = h / 24;
                        h %= 24;
                }
                System.out.print("对应的时间是:");
                if (d > 0) {

                        System.out.print(d + "天");
                }
                if (h > 0) {

                        System.out.print(h + "小时");
                }
                if (m > 0) {

                        System.out.print(m + "分");
                }
                if (s > 0) {

                        System.out.print(s + "秒");

                }

        }
}

8 个回复

倒序浏览
大伙可以来看下,测试题
回复 使用道具 举报
楼主,这个是基础测试题还是入学测试题啊、?我也遇到了,分享下我的程序。。

  1. import java.io.*;
  2. public class Test2 {
  3.         /**
  4.          * 2.从键盘接受一个数字,打印该数字表示的时间,最大单位到天
  5.          *
  6.          */
  7.         public static void main(String[] args) {
  8.                 //从键盘读取数据
  9.                 BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
  10.                 int num = 0;
  11.                 try {
  12.                         num = Integer.parseInt(in.readLine());
  13.                 } catch (NumberFormatException e) {
  14.                         System.out.println("please in put number!");
  15.                         e.printStackTrace();
  16.                 } catch (IOException e) {
  17.                         System.out.println("read wrong!");
  18.                         e.printStackTrace();
  19.                 }
  20.                 calculateTime(num);
  21.         }
  22.        
  23.         //计算时间 并打印
  24.         public static void calculateTime(int num){
  25.                 int day = 0, hour = 0, minute = 0, second = 0;
  26.                
  27.                 second = num % 60;
  28.                 minute = (num / 60) % 60;
  29.                 hour = ((num / 60) / 60) % 24;
  30.                 day = ((num / 60) / 60) / 24;
  31.                
  32.                 if(day > 0)
  33.                         System.out.print(day+"天");
  34.                 if(hour > 0)
  35.                         System.out.print(hour+"小时");
  36.                 if(minute > 0)
  37.                         System.out.print(minute+"分");
  38.                 if(second >0)
  39.                         System.out.print(second+"秒");
  40.         }
  41. }
复制代码




回复 使用道具 举报
哇哦,赞一个噻
回复 使用道具 举报
感谢分享!
回复 使用道具 举报
赞一个、
回复 使用道具 举报
和鹏 中级黑马 2015-4-16 12:09:27
7#
我要是输入负数程序不就bug了
回复 使用道具 举报
嗯嗯。谢谢楼主分享
回复 使用道具 举报
这个应该是社交的基础测试题,我也做过了,计算时间·~用相应公式
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马