2-2 Time类的定义

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

通过本题目的练习可以掌握类与对象的定义;

设计一个时间类Time,私有数据成员有hour(时)、minute(分)、second(秒);

公有成员函数有:setHour(int)设置数据成员hour的值(采用12小时制),非法的输入默认为12;setMinue(int)设置数据成员minute的值,非法输入默认为0;setSecond(int)设置数据成员second的值,非法输入默认为0;setTime(int,int,int)设置时、分、秒三个数据成员的值; showTime()显示时间对象的值。

在主函数main()中调用相应成员函数,使得时间对象的值能从键盘接收,并正确显示。

提示:时、分、秒均按2位数值形式显示 。

Input

输入3个整数,用一个空格间隔

Output

输出 时、分、秒的值,中间用“:”间隔

Sample Input

10 11 12

Sample Output

10:11:12

Hint

输入

58 23 85

输出

12:23:00

按照题目要求建立类,注意异常判断

import java.util.*;

public class Main {
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
Time a = new Time();
a.setTime(cin.nextInt(), cin.nextInt(), cin.nextInt());
a.show();
cin.close();
}
} class Time
{
private int hour,minute,second;
public void setHour(int x)
{
hour = x;
if(hour>12||hour<12)
hour = 12;
}
public void setMinue(int x)
{
minute = x;
if(minute>=60||minute<0)
minute = 0;
}
public void setSecond(int x)
{
second = x;
if(second>=60||second<0)
second = 0;
}
public void setTime(int a,int b,int c)
{
setHour(a);
setMinue(b);
setSecond(c);
}
public void show()
{
System.out.printf("%02d:%02d:%02d\n",hour,minute,second);
}
}

最新文章

  1. 我的Android第三章
  2. CSS基础篇
  3. POJ 3335 Rotating Scoreboard(多边形的核)
  4. putty 中文乱码解决方法
  5. SQL Server数据库事务日志存储序列
  6. Linux下查看文件夹或目录大小
  7. TXMLDocument换行的两种方案
  8. 产生n bit所有可能的序列
  9. 判断进程是64bit还是32bit
  10. C函数调用与栈
  11. JQuery.lazyload 图片延迟加载
  12. vue-eleme 学习笔记
  13. 006-CSS引入外部字体
  14. 如果程序集是从 Web 上下载的,即使它存储于本地计算机,Windows 也会将其标记为 Web 文件,http://go.microsoft.com/fwlink/?LinkId=179545
  15. 在ASP.NET MVC中使用区域来方便管理controller和view
  16. 优秀 Java 程序员写代码的风格
  17. 老男孩Day4作业:员工信息查询系统
  18. [2019BUAA软工]第一次团队作业
  19. formatter easyui 重命名
  20. 小米手机连接adb只显示List of devices attached

热门文章

  1. js 之 call 、 apply
  2. Luogu P2066 机器分配(dp)
  3. vim编辑器操作②
  4. Liferay 7:Liferay内部博客地址
  5. JS---元素隐藏的不同方式
  6. nfs网络共享服务基础
  7. Robbin负载均衡
  8. POJ4852 Ants
  9. CF 848C
  10. Leetcode598.Range Addition II范围求和2