In: Computer Science
Create an application that asks a user to answer 5 math questions.
JAVA
Question:Write a program that asks 5 simple math questions?
Answer:
Math.random() is present in java.lang() library which is the default library in every java program.
Math.random() returns a double value with positive sign.
With the help of typecasting we convert the double value to int.
(int)value [typecasting the value to integer]
Scanner:
Scanner is a class in java.util package used for obtaining the input of the primitive types like int, float, double etc...
nextInt():
nextInt() method scans the next token of the input as an int.
Steps:
I)With the help of Math.random() we select two numbers randomly.
2)Then we ask user to enter the answer.
3)If the answer entered by the user is correct then score is incremented by one.
4)If the answer entered by the user is incorrect then the application says it is incorrect answer and print the correct answer.
5)If user enters incorrect answer score remains same.
6)At the end of the quiz the overall score is printed.
Ouput: