时间溢出分秒和小时

时间:2014-04-03 10:53:52

标签: java string int overflow joptionpane

我需要一些代码来停止我当前的代码
如果起始INT大于结束INT,则产生负数 我知道这里有很多代码我已经注释了我需要帮助的一点(大约一半)但我想提供所有这些所以你可以编译并运行到什么即将与之挣扎

import javax.swing.JOptionPane;
public class race {
 public static void main(String[] args) {


       System.out.println("Course  Start time  End time  Gender  Total time    Name");

  int count = 1;
    while (count<7){
        count++;
  System.out.println( "");
     String colour = JOptionPane.showInputDialog(null,
                 "Competitor Enter your course colour first letter" );

     String Starth = JOptionPane.showInputDialog(null,
             "Competitor Enter your Start time (hours)" );
                     int  Starthour=Integer.parseInt(Starth);          

     String Startm = JOptionPane.showInputDialog(null,
            "Competitor Enter your Start time (mins)" );
                    int  Startmin=Integer.parseInt(Startm);

     String Starts = JOptionPane.showInputDialog(null,
            "Competitor Enter your Start time (secs)" ) ;
                    int Startsec=Integer.parseInt(Starts); 

 String  Endh = JOptionPane.showInputDialog(null,
             "Competitor Enter your End time (hours)" );
                     int  Endhour=Integer.parseInt(Endh);

     String Endm = JOptionPane.showInputDialog(null,
             "Competitor Enter your End Time (mins)" );
                     int Endmin=Integer.parseInt(Endm);    

     String Ends = JOptionPane.showInputDialog(null,
             "Competitor Enter your End Time (secs)" );
                     int Endsec=Integer.parseInt(Ends);    

     String sex = JOptionPane.showInputDialog(null,
             "Male or Female (M/F)" );

     String Name = JOptionPane.showInputDialog(null,
             "Competitor Enter your Full Name (Joe Bloggs)" );


             int Timeh = (Endhour - Starthour); 
             int Timem = (Endmin - Startmin);
             int Times = (Endsec - Startsec);     

//所以这里我需要代码,以便如果我从startsec拿走endsec eg50例如,时间不会打印出-4,而是将其中一个Timem和打印Times作为56 //

 System.out.print(colour);
 System.out.print("     ");
 System.out.print( String.format("%02d",Starthour));
 System.out.print( ":");
 System.out.print( String.format("%02d",Startmin));
 System.out.print( ":");
 System.out.print( String.format("%02d",Startsec));
 System.out.print( "   ");
 System.out.print( String.format("%02d",Endhour));
 System.out.print( ":");
 System.out.print( String.format("%02d",Endmin));
 System.out.print( ":");
 System.out.print( String.format("%02d",Endsec)); 
 System.out.print("      "+sex);
 System.out.print("      "+Timeh);
 System.out.print(":");
 System.out.print(""+Timem);
 System.out.print(":");
 System.out.print(""+Times);
 System.out.print("     "+Name);

 }// end while
 }
  }

0 个答案:

没有答案