解答私信@星火人 爱被打了一巴掌 2023-07-13 10:45 1阅读 0赞 package App; import java.util.*; class Time { private int hour; private int minute; private int second; private int transHour; Scanner reader=new Scanner(System.in); private void SetTime() { System.out.print("Please enter hour, minute, second: "); hour=reader.nextInt(); minute=reader.nextInt(); second=reader.nextInt(); } public Time() { hour=0; minute=0; second=0; } public Time(int hourValue) { hour=hourValue; minute=0; second=0; } public Time(int hourValue, int minuteValue, int secondValue) { hour=hourValue; minute=minuteValue; second=secondValue; } public void ToString12() { switch(hour) { case 13: transHour=1; break; case 14: transHour=2; break; case 15: transHour=3; break; case 16: transHour=4; break; case 17: transHour=5; break; case 18: transHour=6; break; case 19: transHour=7; break; case 20: transHour=8; break; case 21: transHour=9; break; case 22: transHour=10; break; case 23: transHour=11; break; default: break; } System.out.print("12小时制时间:"+transHour+":"+minute+":"+second); if(hour>=0&&hour<=12) System.out.println("AM"); else System.out.println("PM"); } public void ToString24() { System.out.println("24小时制时间:"+hour+":"+minute+":"+second); } } public class App1 { public static void main(String[] args) { Time t1=new Time(); System.out.println("time1:"); t1.ToString24(); t1.ToString12(); Time t2=new Time(22); System.out.println("time2:"); t2.ToString24(); t2.ToString12(); Time t3=new Time(22, 36, 30); System.out.println("time3:"); t3.ToString24(); t3.ToString12(); } }
还没有评论,来说两句吧...