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