📄 secondscalculator.java
字号:
/**
*
* <p>Title: SecondsCalculator</p>
* <p>Description: calculate the seconds according to the input</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author liuyongdong
* @version 1.0
*/
import java.io.*;
import java.util.*;
public class SecondsCalculator {
//ONE - initialize time and reset the time whose value is -1
private static final int ONE = -1;
//RATE1 - the rate from hour to second
private static final int RATE1 = 3600;
// RATE2 - the rate from minute to second
private static final int RATE2 = 60;
//the standard input
private static BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
// the standard output
private static PrintWriter stdOut = new PrintWriter(System.out, true);
//the standard error output
private static PrintWriter stdErr = new PrintWriter(System.err, true);
//initialize hour
private static int hour = ONE;
//initialize minute
private static int minute = ONE;
//initialize second
private static int second = ONE;
//store the totle second
private static int sum;
private static int numOfTokenizer;
private static StringTokenizer tokenizer;
/**
* constructor
* @throws IOException
*/
public SecondsCalculator() throws IOException {
input();
calculator();
}
/**
* get the input
* @throws IOException
*/
public static void input() throws IOException {
do {
stdErr.print("time [ hour : minute : second ]> ");
stdErr.flush();
tokenizer = new StringTokenizer(stdIn.readLine(), ":");
setNumOfTokenizer();
try {
setTime();
if (3 != getNumOfTokenizer() || getHour() < 0
|| getHour() >= 24 || getMinute() < 0
|| getMinute() >= 60 || getSecond() < 0
|| getSecond() >= 60) {
stdErr.println("Invalid input!");
resetTime();
}
} catch (NumberFormatException ex) {
stdErr.println(ex.toString());
} catch (NoSuchElementException nsee) {
stdErr.println("Invalid Enter!");
}
} while (3 != getNumOfTokenizer() || getHour() < 0 || getHour() >= 24
|| getMinute() < 0 || getMinute() >= 60 || getSecond() < 0
|| getSecond() >= 60);
}
private static void resetTime() {
// TODO Auto-generated method stub
hour = ONE;
minute = ONE;
second = ONE;
}
/**
* set the hour
*/
private static void setHour() {
hour = Integer.parseInt(tokenizer.nextToken());
}
/**
* set the minute
*/
private static void setMinute() {
minute = Integer.parseInt(tokenizer.nextToken());
}
/**
* set the second
*/
private static void setSecond() {
second = Integer.parseInt(tokenizer.nextToken());
}
/**
* set the setNumOfTokenizer
*/
private static void setNumOfTokenizer() {
numOfTokenizer = tokenizer.countTokens();
}
/**
* set the time
*/
private static void setTime() {
setHour();
setMinute();
setSecond();
}
/**
* @return the hour
*/
public static int getHour() {
return hour;
}
/**
* @return the minute
*/
public static int getMinute() {
return minute;
}
/**
* @return the second
*/
public static int getSecond() {
return second;
}
/**
* @return the setNumOfTokenizer
*/
public static int getNumOfTokenizer() {
return numOfTokenizer;
}
/**
* calculate the seconds
*/
private static void calculator() {
sum = hour * RATE1 + minute * RATE2 + second;
}
/**
* @return the sum of seconds
*/
public static int getSum() {
return sum;
}
/**
* @return a String to show a output
*/
public String toString() {
return "The number of second is " + getSum();
}
public static void main(String[] args) throws IOException {
SecondsCalculator sc = new SecondsCalculator();
stdOut.println(sc.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -