In: Computer Science
Question: Conversion Program
* This program will ask the user to input meters
* After input, a menu will be displayed:
* 1. Convert to kilometers
* 2. Convert to inches
* 3. Convert to feet
* 4. Quit
* * The user will select the conversion and the converted answer will be displayed.
* The program should redisplay the menu for another selection.
* Quit program when user selects 4.
Hi, my instructor requirement fix the bug in the code and run the code. I try to do some code but still not running thats are below. Please someone is expert review and fix my problem..
public class MetersConversion {
public static void main(String[] args) {
// Initialize variables
int selection;
double meters;
double kilo;
double inch;
double feet;
// Input the distance in Meters to be Converted
Scanner input = new Scanner(System.in);
System.out.print("Enter distance in meters: " + meters);
selection = input.nextInt();
meters = input.nextDouble();
while (selection); {
System.out.print("1. Convert to kilometers");
System.out.print("2. Convert to inches");
System.out.print("3. Convert to feet");
System.out.print("4. Quit the program\n\n");
if(selection == 1){
kilo = meters * 0.001;
}
else if(selection == 2){
inch = meters * 36.37;
}
else if(selection == 3){
feet = meters * 3.201;
}
else if(selConv == 4){
System.out.println("Bye!");
}
}
}
}
Your given code is modified and it is working now...
import java.util.*;
public class MetersConversion {
public static void main(String[] args)
{
// Initialize variables
int selection;
double meters=0;
double kilo;
double inch;
double feet;
// Input the distance in Meters to be Converted
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("\n*** MENU
***\n");
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. Quit the
program\n\n");
System.out.println("Enter your
choice(1-4)");
selection = input.nextInt();
if(selection==4)
{
System.out.println("Bye!");
break;
}
System.out.println("Enter distance
in meters: ");
meters = input.nextDouble();
if(selection == 1)
{
kilo = meters * 0.001;
System.out.println("\n"+meters+" meters is eqquivalent
to "+kilo+" kilometers");
}
else if(selection == 2)
{
inch = meters * 36.37;
System.out.println("\n"+meters+"
meters is eqquivalent to "+inch+" inches");
}
else if(selection == 3)
{
feet = meters * 3.201;
System.out.println("\n"+meters+"
meters is eqquivalent to "+feet+" feets");
}
}
}
}
OUTPUT:
--------------------Configuration: <Default>--------------------
*** MENU ***
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice(1-4)
1
Enter distance in meters:
3000
3000.0 meters is eqquivalent to 3.0 kilometers
*** MENU ***
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice(1-4)
2
Enter distance in meters:
300
300.0 meters is eqquivalent to 10911.0 inches
*** MENU ***
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice(1-4)
3
Enter distance in meters:
300
300.0 meters is eqquivalent to 960.3000000000001 feets
*** MENU ***
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice(1-4)
4
Bye!
Process completed.
N.B: If you face any further problem please contact through comment