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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© w13832027   /  2015-1-21 12:41  /  2621 人查看  /  26 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学习一下
回复 使用道具 举报
这是最简单的,但代码太多了,
回复 使用道具 举报
听说利用字符串表示要简单点,你可以试一下
回复 使用道具 举报
  1. public class Test2 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 //提示用户输入
  8.         System.out.println("请输入你想要转换的秒数(必须为大于0的整数)");
  9.         //定义一个字符读取缓冲流
  10.                 BufferedReader bufr=null;
  11.                         try{
  12.                                 //读取键盘输入
  13.                                 bufr=new BufferedReader(new InputStreamReader(System.in));
  14.                                 String line=null;
  15.                                 //定义要转换的秒数的变量
  16.                                 int seconds=0;
  17.                                 //如果用户输入的值不符合int整数,则循环读取直到符合规则
  18.                                 while((line=bufr.readLine())!=null){
  19.                                         try{
  20.                                                 //如果不能转为int,则抛出NumberFormatException异常
  21.                                                 seconds=Integer.parseInt(line);
  22.                                                 //catch异常
  23.                                         }catch(Exception e){
  24.                                                 System.out.println("请按要求输入");
  25.                                                 //回到循环开始,重新等待用户输入
  26.                                                 continue;
  27.                                         }break;
  28.                                         //调用toDays方法获取要打印的字符串
  29.                                 }System.out.println(toDays(seconds));
  30.                         }catch(IOException e){
  31.                                 System.out.println(e.toString());
  32.                                 //关闭流
  33.                         }finally{
  34.                                 if(bufr!=null)
  35.                                         try{
  36.                                                 bufr.close();
  37.                                         }catch(IOException e){
  38.                                                 System.out.println(e.toString());
  39.                                         }
  40.                         }
  41.                         //实现将秒数转换为×天×时×分×秒格式
  42.         }public static String toDays(int seconds){
  43.                 //定义存储天,时,分,秒的int变量
  44.                 int day;
  45.                 int hour;
  46.                 int min;
  47.                 int second;
  48.                 //定义显示天,时,分,秒的字符串变量
  49.                 String daystr="";
  50.                 String hourstr="";
  51.                 String minstr="";
  52.                 String secondstr="";
  53.                 min=seconds/60;
  54.                 if(min>0){
  55.                         second=seconds%60;
  56.                         secondstr=second+"秒";
  57.                         hour=min/60;
  58.                         min=min%60;
  59.                         minstr=min+"分";
  60.                         if(hour>0){
  61.                                 day=hour/24;
  62.                                 hour=hour%24;
  63.                                 hourstr=hour+"时";
  64.                                 if(day>0){
  65.                                         daystr=day+"天";
  66.                                 }
  67.                         }
  68.                 }return daystr+hourstr+minstr+secondstr;
  69.         }
  70. }
复制代码

我也想了蛮久的
回复 使用道具 举报
w239983684 来自手机 中级黑马 2015-2-28 21:10:38
25#
支持楼主的学习态度。
回复 使用道具 举报
学习了              
回复 使用道具 举报
自己也学着写了几句代码
  1. public static void main(String[] args) {
  2.                 while (true) {
  3.                         try {
  4.                                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  5.                                 System.out.print(">>");
  6.                                 int var = new Integer(bufferedReader.readLine());
  7.                                 int t1 = var / 60;
  8.                                 int t2 = var % 60;
  9.                                 if (t1 < 60 && t1 > 0) {
  10.                                         System.out.print(t1 + "分");
  11.                                 } else if (t1 >= 60) {
  12.                                         System.out.print(t1 / 60 + "小时");
  13.                                 }
  14.                                 if (t2 != 0) {
  15.                                         System.out.print(t2 + "秒");
  16.                                 }
  17.                                 System.out.println();
  18.                         } catch (Exception e) {
  19.                                 e.printStackTrace();
  20.                         }
  21.                 }
  22.         }
复制代码
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马