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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王晓杰 中级黑马   /  2014-12-18 21:19  /  865 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

自己写的一个练习代码 大家指正下  看那有可以改进的地方


  1. package com.itheima;

  2. import java.util.Scanner;

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

  10. * @author Administrator
  11. *
  12. */
  13. public class Test1 {
  14. public static void main(String[]agrs)
  15. {
  16.         Scanner sc = new Scanner(System.in);
  17.         System.out.println("请输入有效的时间");
  18.         int num = sc.nextInt();
  19.         int h=0;
  20.         int m=0;
  21.         int d=0;
  22.         int s=0;
  23.         if(num>3600)
  24.         {
  25.                 int a = num%(3600*24);
  26.                  h = a/3600;
  27.         }
  28.         if(num>60)
  29.         {
  30.                 int a = num%3600;
  31.                 m =a/60;
  32.         }
  33.         if(num>(3600*24))
  34.         {
  35.                 d=num/(3600*24);
  36.         }
  37.         if(num>0)
  38.         {
  39.                 s = num%60;
  40.         }else{
  41.                 System.out.println("请输入大于0的时间");
  42.         }
  43.         System.out.println(d+":"+h+":"+m+":"+s);
  44. }
  45. }
复制代码



3 个回复

倒序浏览
程序可以使用switch看起来比较整洁一些,我跑了你的Demo,没问题
回复 使用道具 举报
lwj123 发表于 2014-12-18 21:27
程序可以使用switch看起来比较整洁一些,我跑了你的Demo,没问题

楼主的if语句里面返回的是boolean类型,如果用switch很麻烦吧
回复 使用道具 举报
用swtich比较好
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马