Sunday, January 1, 2017

// // Leave a Comment

Exercise 3.10 — Game: Addition Quiz

Program that randomly generates an addition question with two integers less than 100.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

import java.util.Scanner;

public class Ex03_10 {
    public static void main(String[] args) {
        // Generate two integers less than 100
        int number1 = (int) (Math.random() * 100);
        int number2 = (int) (Math.random() * 100);

        // Prompt the user to answer "What is number1 + number2?"
        System.out.print("What is " + number1 + " + " + number2 + "? ");
        Scanner input = new Scanner(System.in);
        int answer = input.nextInt();
        // Grade the answer and display the result
        if (number1 + number2 == answer) {
            System.out.println("You are correct!");
        } else {
            System.out.println("Your answer is wrong\n" + number1 + " + " + number2 + " is "
                    + (number1 + number2));
        }
    }
}
Liang Introduction to Java Programming exercise 3.10 solution
Sample program run
Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.