2-2 Time类的定义
来源:互联网 发布:淘宝使用感想 编辑:程序博客网 时间:2024/03/29 23:39
2-2 Time类的定义
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
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
输出 时、分、秒的值,中间用“:”间隔
Example Input
10 11 12
Example Output
10:11:12
Hint
输入
58 23 85
输出
12:23:00
Author
黄晶晶
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int hour = sc.nextInt();int minute = sc.nextInt();int second = sc.nextInt();Time time = new Time();time.setHour(hour);time.setMinute(minute);time.setSecond(second);time.showTime();sc.close();}}class Time {int hour;int minute;int second;public void setHour(int hour) {if (hour < 0 || hour >= 24)hour = 12;this.hour = hour;}public void setMinute(int minute) {if (minute >= 0 && minute <= 59)this.minute = minute;}public void setSecond(int second) {if (second >= 0 && second <= 59)this.second = second;}public void showTime() {System.out.println(format2bit(hour) + ":" + format2bit(minute) + ":"+ format2bit(second));}public String format2bit(int dight) {if (dight / 10 == 0) {return "0" + dight;} else {return String.valueOf(dight);}}}
0 0
- 2-2 Time类的定义
- sdut 2-2 Time类的定义
- [2669]2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- SDUTACM 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- 2-2 Time类的定义
- Sublime Text快捷键
- 使用Executors工厂类创建线程池
- LeetCode 4. Median of Two Sorted Arrays(c++)
- 2015蓝桥杯B组第八题( 移动距离)
- linux上传下载文件
- 2-2 Time类的定义
- Japanese Class <notes> :1
- 类型组合
- AT&T汇编(1)
- mysql 实战 or、in与union all 的查询效率
- 删除链表中的元素
- 关于npm和yarn安装node-sass失败并且依旧想使用NPM或者yarn的完美解决方案
- poj2599
- UltraEdit常用快捷键