Question

I am writing a Java program. My program would ask two questions: (1) 1+1=? (2) 2+1...

I am writing a Java program. My program would ask two questions: (1) 1+1=? (2) 2+1 =? Show one question at a time on the console. The program allows users to input answers via a command-line (no GUI). Give users five seconds to answer each question. The next question would show ONLY when the user answers the question correctly OR when the time is up. Once users answer the question correctly within the given time, the next question needs to show (it wouldn't wait to show until the time is up). Tell users whether the time is up, or they answer the question correctly/wrong. The timer should keep counting down (not restart) when the user answers the question wrong. The timer restarts only when the next question shows. Thank you in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code with screenshot

============================================================================================

import java.util.Scanner;

public class RapidQandA {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long time = System.currentTimeMillis();
        System.out.print("(1) 1+1=?");
        boolean timeUp=true;
        while(System.currentTimeMillis()<time+5*1000){
            int correctAnswer=scanner.nextInt();
            if(correctAnswer==2) {
                System.out.println("Correct answer!");
                timeUp=false;
                break;
            }
        }
        if(timeUp){
            System.out.println("Your time is up!");
        }
        timeUp=true;
        time = System.currentTimeMillis();
        System.out.print("(2) 2+1=?");
        while(System.currentTimeMillis()<time+5*1000){
            int correctAnswer=scanner.nextInt();
            if(correctAnswer==3) {
                System.out.println("Correct answer!");
                timeUp=false;
                break;
            }
        }
        if(timeUp){
            System.out.println("Your time is up!");
        }
    }
}

============================================================================================

Add a comment
Know the answer?
Add Answer to:
I am writing a Java program. My program would ask two questions: (1) 1+1=? (2) 2+1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...

    This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program The first line is the number of questions (just throw this line away this week)...

  • java In this project you will implement a trivia game. It will ask random trivia questions,...

    java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...

  • in c Q3) Write a program to generate three random numbers between 1 and 9 and...

    in c Q3) Write a program to generate three random numbers between 1 and 9 and display them. The user has to enter their sum as soon as possible. These two steps will be repeated continuously. An alarm will be triggered every 5 seconds to display the numb answers so far. If the user did not give a correct answer in 10 seconds, the timer will jump to the main to give an error message "Too slow response!" and terminate...

  • Hello. I am using a Java program, which is console-baed. Here is my question. Thank you....

    Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...

  • Hello I need help with this. I am trying to use JOptionpane to ask 5 questions....

    Hello I need help with this. I am trying to use JOptionpane to ask 5 questions. However I cannot get the program to do anything after an initial selection is made. Here is the assignment. Write an application that creates a quiz, which contains at least five questions about a hobby, popular music, astronomy, or any other personal interest. Each question should be a multiple choice question with at least three options. When a user answers a question correctly, display...

  • Instructions Basically, you will modify the mathq program to make it satisfy a different set of...

    Instructions Basically, you will modify the mathq program to make it satisfy a different set of requirements, and fix what is not working in it. Specifically, imagine that the client for whom we created the mathq program for would now like some changes. To be exact: Part 1: They want it to provide addition and subtraction problems as well as multiplication and division. They still want the choice to be random, as before, but now the problem is randomly multiplication,...

  • Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one...

    Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one receives the length of the sequence, and the second receive the sequence (each number separated by a comma). There may, or may not, be space in between the comma and the number element. Provide a clear button that has an event listener attached to it. When pressed, the two text fields are emptied. The event listener should be defined and implemented as an anonymous...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow f...

    java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT