定义类:
package time; import javax.swing.JOptionPane; public class Time { //声明 private int hour; private int minute; private int second; //----------------------------------------- public void setHour(int newhour){ hour=newhour; } public int getHour(){ return hour; } //------------------------------------------------ public void setMinute(int newminute){ hour=newminute; } public int getMinute(){ return minute; } //------------------------------------------------ public void setSecond(int newsecond){ hour=newsecond; } public int getSecond(){ return second; } //------------------------------------------------ public void d(){ hour=(hour>=0 && hour<24)?hour:-1; minute=(minute>=0 && minute<60)?minute:-1; second=(second>=0 && second<60)?second:-1; //------------------------------------------------------- if(hour!=-1 & minute!=-1 & second!=-1){ String op=String.format("%d:%2d:%2d %s", (hour==12||hour==0)?12:hour%12,minute,second,hour<12?"AM":"PM"); JOptionPane.showMessageDialog(null, op); } else JOptionPane.showMessageDialog(null, "你输入的时间不对,请重新输入","错误消息",+JOptionPane.INFORMATION_MESSAGE); } }
使用:
package time; import javax.swing.JOptionPane; public class Time_1 { public static void main(String[] args){ Time b=new Time(); b.setHour(Integer.parseInt(JOptionPane.showInputDialog("输入小时"))); b.setMinute(Integer.parseInt(JOptionPane.showInputDialog("输入分钟"))); b.setSecond(Integer.parseInt(JOptionPane.showInputDialog("输入秒"))); //------------------------------------------------------------------- b.d(); } }
就是输不出正确的时间,求解
asd8532
相关分类