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.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 3.2 - Game: add three numbers<br /> */<br /><br />import java.util.Scanner;<br /><br />public class Ex03_02 {<br /> public static void main(String[] args) {<br /> // declare variables<br /> int number1 = (int)(System.currentTimeMillis() % 10);<br /> int number2 = (int)(System.currentTimeMillis() / 7 % 10);<br /> int number3 = (int)(System.currentTimeMillis() * 7 % 10);<br /> <br /> // create Scanner to read user input<br /> Scanner input = new Scanner(System.in);<br /> System.out.print(number1 + " + " + number2 + " + " + number3);<br /> // prompt user to enter details<br /> System.out.print("nWhat is " + number1 + " + " + number2 + " + " + number3 + "? ");<br /> int answer = input.nextInt();<br /> System.out.println(number1 + " + " + number2 + " + " + number3 + " = " +<br /> answer + " is " + (number1 + number2 + number3 == answer));<br /> }<br />}<br /> |
Click here to see other solutions to Introduction to Java Programming.