Java Reference
In-Depth Information
Display 2.9
Self-Service Checkout Line
1 import java.util.Scanner;
2 public class SelfService
3 {
4 public static void main(String[] args)
5 {
6 Scanner keyboard = new Scanner(System.in);
7 System.out.println("Enter number of items purchased");
8 System.out.println("followed by the cost of one item.");
9 System.out.println("Do not use a dollar sign.");
10 int count = keyboard.nextInt();
11 double price = keyboard.nextDouble();
12 double total = count*price;
The dot after %. 2f is a period in the
text, not part of the format
specifier.
13 System.out.printf("%d items at $%.2f each.%n", count, price);
14 System.out.printf("Total amount due $%.2f.%n", total);
15 System.out.println("Please take your merchandise.");
16 System.out.printf("Place $%.2f in an envelope %n", total);
17 System.out.println("and slide it under the office door.");
18 System.out.println("Thank you for using the self-service line.");
19 }
20 }
Sample Dialogue
Enter number of items purchased
followed by the cost of one item.
Do not use a dollar sign.
10 19.99
10 items at $19.99 each.
Total amount due $199.90.
Please take your merchandise.
Place $199.90 in an envelope
and slide it under the office door.
Thank you for using the self-service line.
 
Search WWH ::




Custom Search