Java Arena
Step 2 of 6

Calculating with input

Numbers read from the user work like any other numbers once they're converted. A common pattern: read, calculate, then print the result.

Scanner scanner = new Scanner(System.in);
int price = Integer.valueOf(scanner.nextLine());
int count = Integer.valueOf(scanner.nextLine());
int total = price * count;
System.out.println("Total: " + total);
Input
12
4
Output
Total: 48

Storing the result in its own variable (total here) gives it a name, which makes the program easier to read than one long line. It also lets you use the result more than once.

Your task

Challenge 1 of 3

Read two whole numbers and print their sum. For the input 3 and 8 the program prints:

First number:
Second number:
Sum: 11

Challenge 2

Read two whole numbers and print their difference (the first minus the second) and their product. For the input 9 and 4 the program prints:

Difference: 5
Product: 36

Challenge 3

Read three whole numbers and print their sum with a label. For the input 2, 5 and 10 the program prints:

The sum of the numbers is 17