Friday, October 28, 2016

// // Leave a Comment

Exercise 3.2: Game: Add Three Numbers

The program below is the solution to Liang's Introduction to Java Programming (9th Edition) Chapter 3 Exercise 3.2.

Question: The program in Listing 3.1 generates two integers and prompts the user to enter the sum of these two integers. Revise the program to generate three single-digit integers and prompt the user to enter the sum of these three integers.
/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 3.2 - Game: add three numbers
 */

import java.util.Scanner;

public class Ex03_02 {
    public static void main(String[] args) {
        // declare variables
        int number1 = (int)(System.currentTimeMillis() % 10);
        int number2 = (int)(System.currentTimeMillis() / 7 % 10);
        int number3 = (int)(System.currentTimeMillis() * 7 % 10);
        
        // create Scanner to read user input
        Scanner input = new Scanner(System.in);
        System.out.print(number1 + " + " + number2 + " + " + number3);
        // prompt user to enter details
        System.out.print("\nWhat is " + number1 + " + " + number2 + " + " + number3 + "? ");
        int answer = input.nextInt();
        System.out.println(number1 + " + " + number2 + " + " + number3 + " = " +
                answer + " is " + (number1 + number2 + number3 == answer));
    }
}
Solution to Liang's Java Exercise 3.2: Game: Add Three Numbers

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.

Thanks.