The problem
Write a program that converts pounds into kilograms. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms. Below is a representation of a sample run:
Enter a number in pounds: 55.5
55.5 pounds is 25.197 kilograms
Breaking it down
Asking the user to input a number in pounds then storing it to a double pounds
will get us started. Next passing the value to a method named poundsToKilos
will perform the conversion. Finally outputting the value to the console will satisfy the requirements. Did you try to input a character? If so, what happened? If you were writing production level code, you would want to validate user input as a secure software best practice, give it a try!
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number in pounds: ");
input.close();
double pounds = input.nextDouble();
double kilograms = poundsToKilos(pounds);
System.out.println(pounds + " pounds is " + kilograms + " kilograms.");
}
private static double poundsToKilos(double pounds) {
double kilograms = pounds * 0.454;
return kilograms;
}
Output
Enter a number in pounds: 200
200.0 pounds is 90.8 kilograms.