Write a program that uses one or more if statements to solve the following problem:
Prompt the user for a number (type double) and the type of conversion calculation to apply. Once the user supplies the value and the type of conversion, your program should apply the appropriate conversion and display the result. For the conversion type, the user will enter one of the following conversion conversion codes: GL, IC, PK, MK.
Based on the user's input, your program will compute and display the appropriate result. See below for the meaning of the codes:
GL = Gallon to Liter
IC = Inch to Centimeter
PK = Pound to Kilogram
MK = Mile to Kilometer
For instance, if the user inputs GL as their conversion type, the program will then take their numeric input as gallons and converrt it to liters. The liters value would then be output to the user. Please find some additional information below to assist you in solving this problem:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
double number = in.nextDouble();
System.out.println("Enter the conversion code(GL, IC, PK, MK):");
String conversionCode = in.next();
if (conversionCode.equals("GL")) {
System.out.println(number * 3.785 + " liters.");
} else if (conversionCode.equals("IC")) {
System.out.println(number * 2.54 + " centimeters.");
} else if (conversionCode.equals("PK")) {
System.out.println(number / 2.205 + " kilograms.");
} else if (conversionCode.equals("MK")) {
System.out.println(number * 1.609 + " kilometers.");
}
}
}
Comments
Leave a comment