Mathtutor 4-23

posted Mar 5, 2014, 9:37 AM by dldar masud   [ updated Mar 5, 2014, 9:44 AM by Karwan Jacksi ]

package mathtutor;

import java.util.Scanner; // Needed for the Scanner class
import java.util.Random; // Needed tor the Random class

/*
This program demonstrates the Random class.
 */

public class MathTutor {

   
    public static void main(String[] args) 
    {
        int number1;    // A number
        int number2;    // Another number
        int sum;        // The sum of the numbers
        int userAnswer; // The user's answer


       // Create a Scanner object tor keyboard input.
       Scanner keyboard = new Scanner(System.in);
     
       // Create a Random class object.
       Random randomNumbers = new Random();
     
       // Get two random numbers.
       number1 =randomNumbers.nextInt(100);
       number2 = randomNumbers.nextInt(100);
       
       // Display an addition problem.
       System.out.println("What is the answer to the"+
                          "following problem?");
       System.out.print(number2 + " + " +
       number2 + " =? " ) ;
       
       // Calculate the answer,
       sum = number1 + number2;
       
       // Get the user's answer.
       userAnswer = keyboard.nextInt();
       
       // Display the user's results,
       if (userAnswer == sum)
       System.out.println("Correct!");
       else
       {
          System.out.println("Sorry, wrong answer. "+
                             "The correct answer i s "+
                             sum);
       }
        
    }
}

ċ
MathTutor.java
(1k)
Karwan Jacksi,
Mar 5, 2014, 9:37 AM
Comments