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

📄 practical quiz 1.secondscalculator.java

📁 son los ejercicios de ssd3 espero que les ayuden a algunos jeje se la pasan chido.....!!!!!!
💻 JAVA
字号:
import java.io.*;
import java.util.*;

/**
 * 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 Neil
 * @version 1.0.0
 */
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);
	
	/**
     * <code>main</code> method
     *
     * @param args  not used
     */
	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;
	
	/**
	 * Gets a string from standard input and delimits it
	 * into <cite>hour</cite>, <cite>minute</cite> and <cite>second</cite>.
	 */
	private static void getData() throws IOException{

		do {
			stdOut.print("time [hours:minutes:seconds]>");
			stdOut.flush();
			
			StringTokenizer tknzr = new StringTokenizer(stdIn.readLine(),":");
			int tokens = tknzr.countTokens();
			if (tokens != 3) {
				printError();
			}
			else {
				//check if can be delimited correctly
				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);
	}
	
	/**
	 * Print the error message "Invalid input"
	 */
	private static void printError() {
		
		stdErr.println("Invalid input");
	}
}

⌨️ 快捷键说明

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