Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. /*
  2.  *   The code written in this class was created by Jacob Cuomo
  3. */
  4.  
  5.  
  6. import java.util.Scanner;
  7.  
  8. public class Conversion {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner input = new Scanner(System.in);
  12.         int num = 0;
  13.         boolean isNum = false;
  14.  
  15.         System.out.println("Enter a distance in meters: ");
  16.         // This loop checks to determine if the number entered is positive and is actually
  17.         // a number.
  18.         do {
  19.             System.out.println("The number you enter should be positive");
  20.             while (!input.hasNextInt()) {
  21.                 System.out.println("Please enter a valid number");
  22.                 input.next();
  23.             }
  24.             num = input.nextInt();
  25.         } while (num <= 0);
  26.  
  27.         // The following loop will make sure the menu continually appears.
  28.         int option = 0;
  29.         while (option != 4) {
  30.             displayMenu();
  31.             do {
  32.                 System.out.println("Enter a number 1-4");
  33.                 while (!input.hasNextInt()) {
  34.                     System.out.println("Please enter a valid number");
  35.                     input.next();
  36.                 }
  37.                 option = input.nextInt();
  38.             } while(!(option >= 1 && option <= 4));
  39.             switch(option) {
  40.                 case 1:
  41.                     showKilometers(num); break;
  42.                 case 2:
  43.                     showInches(num); break;
  44.                 case 3:
  45.                     showFeet(num); break;
  46.                 case 4:
  47.                     System.exit(0);
  48.                 default:
  49.                     System.out.println("Unsure how you got here.");
  50.             }
  51.         }
  52.     }
  53.  
  54.     private static void displayMenu() {
  55.         System.out.println("Choose an option from the menu as you like.");
  56.         System.out.println("1. Convert to Kilometers");
  57.         System.out.println("2. Convert to Inches");
  58.         System.out.println("3. Convert to Feet");
  59.         System.out.println("4. Exit Program");
  60.     }
  61.  
  62.     private static void showKilometers(int meters) {
  63.         double kilometers = meters * 0.001;
  64.         System.out.println(meters + " meters is " + kilometers + " kilometers.");
  65.     }
  66.  
  67.     private static void showInches(int meters) {
  68.         double inches = meters * 39.37;
  69.         System.out.println(meters + " meters is " + inches + " inches.");
  70.     }
  71.  
  72.     private static void showFeet(int meters) {
  73.         double feet = meters * 3.281;
  74.         System.out.println(meters + " meters is " + feet + " feet.");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement