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

📄 secondscalculator.java

📁 ssd3 Practical Quiz 1
💻 JAVA
字号:
import java.io.*;
import java.util.*;

public class SecondsCalculator {

	public static BufferedReader stdIn = 
		new BufferedReader(new InputStreamReader(System.in));
	
	public static PrintWriter stdOut = 
		new PrintWriter(System.out,true);
	
	public static PrintWriter stdErr = 
		new PrintWriter(System.out,true);
	

	public static void main(String[] args) throws IOException{
		
		getData();
		
		int total = hours * 3600 + minutes * 60 + seconds;
		stdOut.println("The number of seconds is: " + total);
	}

	private static int hours = 0;
	private static int minutes = 0;
	private static int seconds = 0;
	

	private static void getData() throws IOException{

		do {
			stdOut.print("time [hours:minutes:seconds]>");
			stdOut.flush();
			
			StringTokenizer tknzr = new StringTokenizer(stdIn.readLine(),":");
			int ns = tknzr.countTokens();
			if (ns != 3) {
				printError();
			}
			else {
				
				try {
					hours = Integer.parseInt(tknzr.nextToken());
					minutes = Integer.parseInt(tknzr.nextToken());
					seconds = Integer.parseInt(tknzr.nextToken());
					
					if (hours>=0 && hours <24 && minutes>=0 && minutes<60 && seconds>=0 && seconds<60) {
						return;
					}
					else {
						printError();
					}
				} catch (NumberFormatException nfe) {
					stdOut.println(nfe);
				}
			}
		} while (true);
	}
	
	
	private static void printError() {
		
		stdErr.println("Invalid input");
	}
}

⌨️ 快捷键说明

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