Java Reference
In-Depth Information
2.5
a) // Calculate the product of three integers
b) Scanner input = new Scanner(System.in);
c) int x, y, z, result;
or
int x;
int y;
int z;
int result;
d) System.out.print( "Enter first integer: " );
e) x = input.nextInt();
f) System.out.print( "Enter second integer: " );
g) y = input.nextInt();
h) System.out.print( "Enter third integer: " ) ;
i) z = input.nextInt();
j) result = x * y * z;
k) System.out.printf( "Product is %d%n" , result);
2.6
The solution to Self-Review Exercise 2.6 is as follows:
1
// Ex. 2.6: Product.java
2
// Calculate the product of three integers.
3
import java.util.Scanner; // program uses Scanner
4
5
public class Product
6
{
7
public static void main(String[] args)
8
{
9
// create Scanner to obtain input from command window
10
Scanner input = new Scanner(System.in);
11
12
int x; // first number input by user
13
int y; // second number input by user
14
int z; // third number input by user
15
int result; // product of numbers
16
17
System.out.print( "Enter first integer: " ); // prompt for input
18
x = input.nextInt(); // read first integer
19
20
System.out.print( "Enter second integer: " ); // prompt for input
21
y = input.nextInt(); // read second integer
22
23
System.out.print( "Enter third integer: " ); // prompt for input
24
z = input.nextInt(); // read third integer
25
26
result = x * y * z; // calculate product of numbers
27
28
System.out.printf( "Product is %d%n" , result);
29
} // end method main
30
} // end class Product
Enter first integer: 10
Enter second integer: 20
Enter third integer: 30
Product is 6000
Search WWH ::




Custom Search