Results 1 to 6 of 6
  1. #1
    Evana's Avatar
    Evana is offline Member
    Join Date
    Nov 2016
    Location
    Belgium
    Posts
    7
    Rep Power
    0

    Question BMI practice code

    Hi, I have written a code to calculate someone's bmi. But at the end of our course, we got the solution and there was a part that I did not understand. Namely why the /100.0 is written after "double length = keyboard.nextInt()"

    Java Code:
    import java.util.*;
    
    
    public class MyBmi {
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
    
            System.out.print("Enter your name: ");
            String name = keyboard.nextLine();
    
            System.out.print("Enter your length in cm: ");
            double length = keyboard.nextInt() /100.0 ;         // why is this /100.0 ??
    
            System.out.print("Enter your weight in kg: ");
            int weight = keyboard.nextInt()  ;
    
            double bodyMassIndex = weight / ( length * length);
    
            System.out.printf("Hello %s, your BMI is %.1f. ", name, bodyMassIndex);
            if ( bodyMassIndex < 18.5) {
                System.out.println("You have underweight.");
            } else if (bodyMassIndex >= 18.5 && bodyMassIndex < 25) {
                System.out.println("You have a normal weight.");
            } else if (bodyMassIndex >= 25 && bodyMassIndex < 30) {
                System.out.println("You have overweight.");
            } else if (bodyMassIndex >= 30 && bodyMassIndex < 40) {
                System.out.println("You are obese.");
            }
            System.out.println();  //blank line
            System.out.println( "Hi " +name  + "." + " It's important to stay healthy, remember that!");  // + sign to concat !!!
        }
    }

    Can anyone help me with this. I want to say, I'm a complete beginner. So I hope I understand the answer

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: BMI practice code

    That formula wants the length measured in metres and your program wants the length input in centimetres.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Evana's Avatar
    Evana is offline Member
    Join Date
    Nov 2016
    Location
    Belgium
    Posts
    7
    Rep Power
    0

    Default Re: BMI practice code

    got it, thanks JosAH, you're a lifesaver

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: BMI practice code

    Quote Originally Posted by Evana View Post
    you're a lifesaver
    I know ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: BMI practice code

    Yeah. Give the man a cheroot. :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: BMI practice code

    Quote Originally Posted by jim829 View Post
    Yeah. Give the man a cheroot. :)
    A cheroot? Yuck, no thanks; I only smoke Van Nelle Zware Shag or an occasional Brandaris ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. What is the best practice in wrting the same code
    By shivam0101 in forum New To Java
    Replies: 2
    Last Post: 08-15-2012, 02:07 AM
  2. Multiplication practice app
    By Mikki in forum New To Java
    Replies: 4
    Last Post: 06-24-2012, 11:38 PM
  3. GUI - is this bad practice?
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-01-2011, 12:40 PM
  4. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 04:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •