⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 secondscalculator.java

📁 SSD3 Practical quiz1
💻 JAVA
字号:
import java.util.*;
import java.io.*;
/**
 * This application will read a time interval expressed in hours, minutes,
 *  and seconds from the keyboard and then display the total number 
 *  of seconds in the specified time interval. 
 *
 * @author  Guimin Lin
 * @version 1.0.0
 */

public class SecondsCalculator {

   
    	 private static BufferedReader  stdIn =
    	        new BufferedReader(new  InputStreamReader(System.in));

    	    private static PrintWriter  stdErr =
    	        new PrintWriter(System.err, true);

    	    private static PrintWriter  stdOut =
    	        new PrintWriter(System.err, true);

    	       	    
    	    
    	    /**
    	     * Tests method <code>readInteger</code>
    	     *
    	     * @param args  not used
    	     * @throws IOException if error reading from standard input.
    	     */
    	    public static void  main(String[] args) throws IOException {
    	    	 final int value;
    	    	 StringTokenizer  stringtokenizer;
    	    	 int hour = 0;
    	    	 int minute = 0;
    	    	 int second = 0;
    	    	
    	    	do{   	    	
                    stdErr.print("Time [hours:minutes:seconds]> ");
                    stdErr.flush();
                   
                    /*
        	    	 * get the the string from input
        	    	 */
                    
                    stringtokenizer =
                        new  StringTokenizer(stdIn.readLine(),":");
               
                    if(stringtokenizer.countTokens() != 3){
                    	stdErr.println("Invalid input");
                    continue;
                    }
                                 
                   
    	    	/*
    	    	 * get the hour,minute and second value
    	    	 */
    	    		try{
    	    			
                     hour = Integer.parseInt(stringtokenizer.nextToken());
                     minute = Integer.parseInt(stringtokenizer.nextToken());
                     second = Integer.parseInt(stringtokenizer.nextToken());
                     
    	    		}catch(NumberFormatException nfe){
    	    			stdErr.println(nfe);
    	    			continue;
    	    		}
                      /*
          	    	 * test the hour,minute and second values to known whether they are valid or not
          	    	 * and then print the corresponing output 
          	    	 */
                    if(hour < 0 || hour >= 24){                    	
                    	 stdErr.println("Invalid input");
                    	 continue;
                     }else if(minute <0 ||minute >=60){
                   		 stdErr.println("Invalid input");
                   		 continue;
                     }else if(second <0 || second >=60){
                      	 stdErr.println("Invalid input");
                      	 continue;
                     }else {
                    	value = hour * 3600 + minute * 60 + second;
                    	stdOut.println("The number of seconds is: " + value);
                    	break;
                    }

    	    	}while(true);
    
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -