Answer:
- import java.util.Scanner;
- import java.util.Random;
-
- public class Main {
- public static void main(String[] args) {
-
- int rand1, rand2, sum, ans;
- Random rand = new Random();
-
- rand1 = rand.nextInt(1000);
- rand2 = rand.nextInt(1000);
- sum = rand1 + rand2;
-
- System.out.println(rand1 + " + " + rand2);
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter your answer: ");
- ans = input.nextInt();
-
- if(ans == sum){
- System.out.println("Congratulations! You did it right!");
- }else{
- System.out.println("Wrong answer. The correct answer should be " + sum);
- }
- }
- }
Step-by-step explanation:
Firstly import Scanner and Random libraries which will be used to get input number and to generate random value (Line 1 -2).
Next declare all the necessary variables as given in pseudo-code (Line 7 - 8)
Then use nextInt method to randomly generate integer and assign them to rand1 and rand 2, respectively (Line 10 - 11). Next, sum up the two random values and assign it to variable ans (Line 12).
Display the two random numbers (Line 14) and create a Scanner object and prompt user input for answer (Line 16- 18).
Create an if-else condition to check if the input answer (ans) is equal to the real answer (sum) and display the appropriate message accordingly (Line 20 - 24).