Zehdari

Untitled

Apr 7th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class mpg {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner scan = new Scanner(System.in);
  10. DecimalFormat format = new DecimalFormat("###,###,##0.0");
  11.  
  12. double init, fin, gal, mpg;
  13. System.out.println("Welcome to the Miles per Gallon Calculator!");
  14.  
  15. init = 12;
  16.  
  17. while (init >= 0){
  18. System.out.print("\nEnter the INITIAL ODOMETER READING or a negative number to STOP: ");
  19. init = scan.nextDouble();
  20.  
  21. if(init >= 0){
  22. System.out.print("\nEnter the FINAL ODOMETER READING: ");
  23. fin = scan.nextDouble();
  24. System.out.print("\nEnter the number of GALLONS OF GAS used: ");
  25. gal = scan.nextDouble();
  26.  
  27. mpg = (fin-init)/gal;
  28. System.out.print("\n\tMiles per Gallon: " +format.format(mpg) + "\n");
  29. }
  30.  
  31.  
  32. }
  33. System.out.println("\nBYE!!");
  34.  
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment