Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projects;
- import java.time.Instant;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- import java.util.Scanner;
- public class TimeBomb {
- static String[][] questions = {
- {
- "What is the square root of 121?",
- "[A] 10\n[B] 11\n[C]13\n\nYour answer: ",
- "b"
- },
- {
- "What is the decimal of 10100?",
- "[A] 100\n[B] 20\n[C] 18\n\nYour answer: ",
- "b"
- },
- {
- "What is the binary number of 108?",
- "[A] 1101100\n[B] 0101100\n[C] 11111000\n\nYour answer: ",
- "a"
- },
- {
- "What is the octal number of 4848?",
- "[A] 11360 base 8\n[B] 21450 base 8\n[C] 27D base 16\n\nYour answer: ",
- "a"
- },
- {
- "What is the hexadecimal number of 9000?",
- "[A] 12F0 base 16\n[B] 2328 base 16\n[C] 6C base 16\n\nYour answer: ",
- "b"
- },
- };
- static String[] answers = new String[5];
- static int corrects = 0, wrongs = 0, index = 0;
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String leftAlignFormat = "| %-10d | %-16s | %-16s | %-16s | %-16s |%n";
- Date startDate = Date.from(Instant.now()), endDate = null;
- String timeStart = formatTime(LocalDateTime.now());
- Date start = null;
- Date end = null;
- int index = 0;
- // loop through all the questions
- while (index < questions.length) {
- // each start of the loop, we get the time (in milliseconds)
- Instant _instant = Instant.now();
- // we convert that time into a date & time format
- LocalDateTime _start = LocalDateTime.ofInstant(_instant, ZoneId.systemDefault());
- // this here, records when the user started answering the question.
- start = Date.from(_instant);
- // grab the question, options and answer from our 2-dimensional array
- String question = questions[index][0];
- String options = questions[index][1];
- String answer = questions[index][2];
- System.out.println("Question #" + (index + 1));
- System.out.println(question);
- System.out.print(options);
- String in = input.nextLine();
- if ( in .toLowerCase().equals(answer.toLowerCase()))
- corrects += 1; // if correct, increment our 'corrects' counter
- else
- wrongs += 1; // if wrong, increment our 'wrongs' counter
- end = Date.from(Instant.now());
- answers[index] = String.format("%s|%s|%s|%s", in .toUpperCase(), in .toLowerCase().equals(answer.toLowerCase()) ? "Corrrect" : "Wrong",
- formatTime(_start).split("@")[1],
- computeDuration(start, end));
- index += 1;
- System.out.println();
- }
- input.close();
- endDate = Date.from(Instant.now());
- /*
- After answering all the questions, the user's performance will be displayed in this table:
- +----------------------------------------------------------------------------------------+
- | Time bomb game started on June 23, 2021 @04:21 PM |
- +------------+------------------+------------------+------------------+------------------+
- | Question # | Your Answer | Remarks | Time | Duration |
- +------------+------------------+------------------+------------------+------------------+
- | 1 | A | Corrrect | 04:21 PM | 2 seconds |
- | 2 | C | Wrong | 04:21 PM | 1 seconds |
- | 3 | B | Wrong | 04:21 PM | 2 seconds |
- | 4 | A | Corrrect | 04:21 PM | 1 seconds |
- | 5 | B | Wrong | 04:21 PM | 1 seconds |
- +------------+------------------+------------------+------------------+------------------+
- */
- System.out.println("+----------------------------------------------------------------------------------------+");
- System.out.printf("| Time bomb game started on %-60s |\n", timeStart);
- System.out.println("+------------+------------------+------------------+------------------+------------------+");
- System.out.println("| Question # | Your Answer | Remarks | Time | Duration |");
- System.out.println("+------------+------------------+------------------+------------------+------------------+");
- int counter = 1;
- for (String answer: answers) {
- // split the user's answers
- String[] _data = answer.split("\\|");
- String _answer = _data[0];
- String _remarks = _data[1];
- String _time = _data[2];
- String _duration = _data[3];
- System.out.printf(leftAlignFormat, counter, _answer, _remarks, _time, _duration);
- counter += 1;
- }
- System.out.println("+------------+------------------+------------------+------------------+------------------+");
- System.out.printf("Number of correct answers: %d\t\tNumber of wrong answers: %d\n", corrects, wrongs);
- System.out.printf("Total Duration: %s\n", computeDuration(startDate, endDate));
- }
- static String formatTime(LocalDateTime localDateTime) {
- return localDateTime.format(DateTimeFormatter.ofPattern("MMMM dd, yyyy @hh:mm a"));
- }
- static String computeDuration(Date start, Date end) {
- long diff = end.getTime() - start.getTime();
- diff = (diff / 1000) % 60;
- return (diff > 60) ? computeDurationMinutes(start, end) : String.format("%d second(s)", diff);
- }
- static String computeDurationMinutes(Date start, Date end) {
- long diff = end.getTime() - start.getTime();
- diff = (diff / (60 * 1000)) % 60;
- return String.format("%d minute(s)", diff);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement