2-2 Time类的定义

WA:不懂。。。

//import java.util.*;

import java.util.Arrays;
import java.util.Scanner;  

class Time {// 构造time类
	int h;
	int m;
	int s;

	/*public void SetHour(int h) {
		if (h >= 0 && h <= 12) {
			this.h = h;
		} else {
			this.h = 12;
		}
		// return this.hour;

	}

	public void SetMinute(int m) {
		if (m >= 0 && m <= 60) {
			this.m = m;
		} else {
			this.m = 0;
		}
		// return this.minute;

	}

	public void SetSecond(int s) {
		if (s >= 0 && s <= 60) {
			this.s = s;
		} else {
			this.s = 0;
		}

	}
	*/
	//简化版
	Time(int h,int m,int s){
		this.h=h;
		this.m=m;
		this.s=s;

	}
	public void SetHour(int h){
		if(h<0||h>12)
			this.h=12;
	
		
			
	}
	public void SetMinute(int m){
		if(m<0||m>60)
			this.h=0;
		
	}
	public void SetSecond(int s){
		if(s<0||s>60)
			this.s=0;
	}

	public void Show() {

		System.out.printf("%02d:%02d:%02d\n",h,m,s);
//The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, 
//int, int, int)
	}

}

public class Main {
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		/*
		 * Time time=new Time();//构造方法的默认的,内无语句。 int h2=input.nextInt(); int
		 * m2=input.nextInt(); int s2=input.nextInt();
		 * time.SetandShowTime(h2,m2,s2);//调用方法
		 */
		String s = input.nextLine();// 包含空格
		String str[] = s.split(" ");
		int h2 = Integer.parseInt(str[0]);// 把字符转换成整型
		int m2 = Integer.parseInt(str[1]);
		int s2 = Integer.parseInt(str[2]);
		Time t = new Time(h2,m2,s2);
		t.SetHour(h2);
		t.SetMinute(m2);
		t.SetSecond(s2);
		t.Show();

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值