java TimeTest
檔名:Time.java
public class Time
{
private int hour;
private int minute;
private int second;
public void setTime( int h, int m, int s )
{
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
}
public String toUniversalString()
{
return String.format( "%02d:%02d:%02d", hour, minute, second );
}
public String toString()
{
return String.format( "%02d:%02d:%02d",
( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
minute, second, ( hour < 12 ? "AM" : "PM") );
}
}
檔名:TimeTest.java
public class TimeTest
{
public static void main ( String args[] )
{
Time time = new Time();
System.out.print( "初始國際格式時間為:" );
System.out.println( time.toUniversalString() );
System.out.print( "初始標準格式時間為:" );
System.out.println( time.toString() );
System.out.println();
time.setTime( 13, 27, 6);
System.out.print( "設定後國際格式時間為:" );
System.out.println( time.toUniversalString() );
System.out.print( "設定後標準格式時間為:" );
System.out.println( time.toString() );
System.out.println();
time.setTime( 99, 99, 99 );
System.out.println( "在嘗試非法設定值之後");
System.out.print( "設定非法值後國際格式時間為:" );
System.out.println( time.toUniversalString() );
System.out.print( "設定非法值後標準格式時間為:" );
System.out.println( time.toString() );
}
}
留言列表