Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The code written in this class was created by Jacob Cuomo
- */
- import java.util.Scanner;
- public class Conversion {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int num = 0;
- boolean isNum = false;
- System.out.println("Enter a distance in meters: ");
- // This loop checks to determine if the number entered is positive and is actually
- // a number.
- do {
- System.out.println("The number you enter should be positive");
- while (!input.hasNextInt()) {
- System.out.println("Please enter a valid number");
- input.next();
- }
- num = input.nextInt();
- } while (num <= 0);
- // The following loop will make sure the menu continually appears.
- int option = 0;
- while (option != 4) {
- displayMenu();
- do {
- System.out.println("Enter a number 1-4");
- while (!input.hasNextInt()) {
- System.out.println("Please enter a valid number");
- input.next();
- }
- option = input.nextInt();
- } while(!(option >= 1 && option <= 4));
- switch(option) {
- case 1:
- showKilometers(num); break;
- case 2:
- showInches(num); break;
- case 3:
- showFeet(num); break;
- case 4:
- System.exit(0);
- default:
- System.out.println("Unsure how you got here.");
- }
- }
- }
- private static void displayMenu() {
- System.out.println("Choose an option from the menu as you like.");
- System.out.println("1. Convert to Kilometers");
- System.out.println("2. Convert to Inches");
- System.out.println("3. Convert to Feet");
- System.out.println("4. Exit Program");
- }
- private static void showKilometers(int meters) {
- double kilometers = meters * 0.001;
- System.out.println(meters + " meters is " + kilometers + " kilometers.");
- }
- private static void showInches(int meters) {
- double inches = meters * 39.37;
- System.out.println(meters + " meters is " + inches + " inches.");
- }
- private static void showFeet(int meters) {
- double feet = meters * 3.281;
- System.out.println(meters + " meters is " + feet + " feet.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement