I ran across this site while trying to Google search for help. I noticed this site showed up a lot of times and answered the questions I had, so I decided to join. I'm writing a program from class but I'm having some trouble. I would like to loop this program if the user enters a selection that's not on the menu. I believe I need to use a while loop with the condition (number > 6)?
Also I can't figure out how to get the output to display: " _ feet are equal to _ inches"
The parts I can't figure out how to display are highlighted in red in the statement above.
Here is my program, any input is greatly appreciated! Thank you.
import java.util.Scanner;
public class homework {
public static void main (String [] args){
Scanner keyboard = new Scanner(System.in);
int value;
// Display menu
System.out.println("Welcome to the Distance Conversion System");
System.out.println(" Main menu ");
System.out.println(" ---------------- ");
System.out.println(" 1- Convert from Feet to Inch. ");
System.out.println(" 2- Convert from Inch to Feet. ");
System.out.println(" 3- Convert from Yard to Feet. ");
System.out.println(" 4- Convert from Feet to Yard. ");
System.out.println(" 5- Convert from Yard to Mile. ");
System.out.println(" 6- Convert from Mile to Yard. ");
System.out.println(" 0- Exit ");
System.out.print("Enter your choice: ");
value = keyboard.nextInt();
switch (value){
case 1:
System.out.print("Enter a distance in feet to be converted to inches: ");
value = keyboard.nextInt();
System.out.println(" feet are equal to " + value * 12);
break;
case 2:
System.out.print("Enter a distance in inches to be converted to feet: ");
value = keyboard.nextInt();
System.out.println("inches are equal to " + value/12);
break;
case 3:
System.out.print("Enter a distance in yards to be converted to feet: ");
value = keyboard.nextInt();
System.out.print(" yards are equal to " + value * 3);
break;
case 4:
System.out.print("Enter a distance in feet to be converted to yards: ");
value = keyboard.nextInt();
System.out.print(" feet are equal to " + (double) value/3.0);
break;
case 5:
System.out.print("Enter a distance in yards to be converted to miles: ");
value = keyboard.nextInt();
System.out.print(" yards are equal to " + (double) value/1760.0);
break;
case 6:
System.out.print("Enter a distance in miles to be converted to yards: ");
value = keyboard.nextInt();
System.out.print(" miles are equal to " + value * 1760 );
break;
case 0:
break;
default:
System.out.println("Invalid selection, please try again..!");
value = keyboard.nextInt();
}
}
}