?? secondscalculator.java
字號(hào):
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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -