黑马程序员技术交流社区

标题: 编程详解 [打印本页]

作者: 透过生活    时间: 2014-1-21 15:15
标题: 编程详解
本帖最后由 透过生活 于 2014-2-16 18:47 编辑

从键盘接受一个数字,打印该数字表示的时间,最大单位到天

例如:

键盘输入6,打印6秒

键盘输入60,打印1分

键盘输入66,打印1分6秒

键盘输入666,打印11分6秒

键盘输入3601,打印1小时1秒

键盘输入86440,打印1天40秒
作者: ↑↓ME→←    时间: 2014-1-21 15:19
这是考试的题
作者: 奋斗的小雨    时间: 2014-1-21 16:14
我的基础测试里貌似也有这道题,不过我还没学到这额,慢慢来吧
作者: 宋星    时间: 2014-1-21 16:23
  1. package com.itheima;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;

  5. public class Test1 {
  6.         public static void main(String[] args) {
  7.                
  8.                 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
  9.                 String line=null;
  10.                 System.out.println("请输入数字:");
  11.                 try {
  12.                
  13.                         while((line=bufr.readLine())!=null)
  14.                         {
  15.                                
  16.                                 if(line.equals("over"))
  17.                                         break;
  18.                                 int input;
  19.                                 try {                //这个try...catch为处理键盘录入的字符是非数字的异常
  20.                                                 //将字符串中的数字转换为int类型
  21.                                         input = Integer.parseInt(line);
  22.                                        
  23.                                         number2time(input);
  24.                                 } catch (Exception e) {
  25.                                
  26.                                         System.out.println("输入的不是数字,请重新输入");
  27.                                 }
  28.                                
  29.                         }
  30.                         bufr.close();
  31.                 } catch (IOException e) {               
  32.                
  33.                         System.out.println("读取失败");
  34.                 }
  35.         }
  36.                         //将数字转换为时间的方法
  37.         public static void number2time(int input){
  38.                 int days=0;
  39.                 int hours=0;
  40.                 int minutes=0;
  41.                 int seconds=0;
  42.                 //定义一个StringBuilder,装输出数据
  43.                 StringBuilder time=new StringBuilder();
  44.                 if(input<0){
  45.                         //提醒数字不能小于零
  46.                         System.out.println("数字不能小于零,请重新输入");
  47.                 }
  48.                 else if(input>=60){
  49.                         minutes=input/60;               
  50.                         seconds=input%60;               
  51.                         if(minutes>=60)
  52.                         {
  53.                                 hours=minutes/60;               
  54.                                 minutes=minutes%60;               
  55.                                 if(hours>=24){
  56.                                         days=hours/24;               
  57.                                         hours=hours%24;               
  58.                                 }
  59.                         }
  60.                 }
  61.                 else {
  62.                         seconds=input;                //当传入的整数在0~60之间时,就只有秒数
  63.                 }
  64.                
  65.                 if(days!=0){
  66.                         time.append(days+"天");
  67.                 }
  68.                 if(hours!=0){
  69.                         time.append(hours+"时");
  70.                 }
  71.                 if(minutes!=0){
  72.                         time.append(minutes+"分");
  73.                 }
  74.                 if(seconds!=0){
  75.                         time.append(seconds+"秒");
  76.                 }
  77.                 System.out.println(time);
  78.         }
  79. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2