Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /**
  2. * A simple Body Mass Index (BMI) calculator program that uses
  3. * if-else statements to determine the BMI status of an individual.
  4. *
  5. * @author Tyler Wargo
  6. * @version 1.0
  7. */
  8.  
  9. import java.util.Scanner;
  10. public class BMI
  11. {
  12. public static void main(String[] args)
  13. {
  14. //Declare & Initialize Variables/Objects
  15. String category;
  16. Scanner in = new Scanner(System.in);
  17.  
  18. //Display BMI Calculator heading
  19. System.out.println("==========================");
  20. System.out.println("Body Mass Index Calculator");
  21. System.out.println("==========================\n");
  22.  
  23. //Get inputs for BMI results
  24. System.out.print("Please enter your first and last name (e.g. John Williams): ");
  25. String name = in.nextLine();
  26. System.out.print("Please enter your weight in pounds (e.g. 128): ");
  27. double lb_weight = in.nextDouble();
  28. System.out.print("Please enter your height in feet and inches (e.g. 5 11): ");
  29. String feet = in.next();
  30. String inches = in.next();
  31.  
  32. //Calculate variables for BMI
  33. double m_height = ((Double.parseDouble(feet) * 12) + Double.parseDouble(inches)) * 0.0254; //Convert feet to inches, parse strings, and convert them to a height in meters using a factor
  34. double kg_weight = lb_weight * 0.4535; //Convert weight in pounds to weight in kilograms
  35. double bmi = kg_weight / (m_height * m_height); //Calculate BMI (Also squares the heights)
  36.  
  37. //Check if BMI if within a certain range and placing it within a category
  38. if(bmi >= 29.9)
  39. {
  40. category = "Obese";
  41. }
  42. else if(bmi >= 25.0)
  43. {
  44. category = "Overweight";
  45. }
  46. else if(bmi >= 18.5)
  47. {
  48. category = "Normal or Healthy Weight";
  49. }
  50. else
  51. {
  52. category = "Underweight";
  53. }
  54.  
  55. //Display BMI Results heading
  56. System.out.println("\n=======================");
  57. System.out.println("Body Mass Index Results");
  58. System.out.println("=======================\n");
  59.  
  60. //Display/Output BMI results and information
  61. System.out.println("Name: " + name);
  62. System.out.println("Height (m): " + m_height);
  63. System.out.println("Weight (kg): " + kg_weight);
  64. System.out.println("BMI: " + bmi);
  65. System.out.print("Category: " + category);
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement