mardi 29 décembre 2015

If statement not triggered even when equal to true

I have attempted to write a program which tells your BMI(Body mass index) and if you're over/under weighted or not when you enter your weight and height.

Everything went perfectly until the if-else statement, no matter what the "BMI" is it always displays "You are under weighted."

Ex:

Would you like to use Standard/U.S
Standard
Whats your weight?
200
What's your height?
50
Your BMI is 800
You are underweighted!

Here is the area I believe is causing the error:

if(which==0)
        { 
            int old = 1;
         for(double faf = 0;faf<=BMI2;faf=faf+0.5) 
         {
             double fof = 0;
             fof++;
             if(fof<37 && old!=0)   
             {
              System.out.println("You are underweighted!");
              old = 0;
             }
             else if(fof<50 && old!=0)
             {
              System.out.println("You are Normal Weighted!");
              old = 0;
             }
             else if(fof<60 && old!=0)
             {
              System.out.println("You are Overweighted!");  
              old = 0;
             }
             else if(fof>=60)
             {
              System.out.println("Obesity!");
              old = 0;
             }
         }
        }
         else if(which==2)
         {
             int old = 1;
             for(double faf = 0;faf<=BMI;faf=faf+0.5) 
             {
                 double fof = 0;
                 fof++;

                 if(fof<37 && old!=0)   
                 {
                  System.out.println("You are underweighted!");
                  old = 0;
                 }
                 else if(fof<50 && old!=0)
                 {
                  System.out.println("You are Normal Weighted!");   
                  old = 0;
                 }
                 else if(fof<60 && old!=0)
                 {
                  System.out.println("You are Overweighted!");
                  old = 0;
                 }
                 else if(fof>=60)
                 {
                  System.out.println("Obesity!");
                  old = 0;
                 }
         }

         } 

and here is the full code in case that wasn't the area causing the problem:

import java.util.Scanner; 
import java.text.*;
public class BMI {

    public static void main(String[] args) {
        Scanner lol = new Scanner(System.in);
        int which = 1;
        System.out.println("Would you like to use Standard/U.S scale?");
        String choice = lol.next();
        System.out.println("Whats your weight?");
        double weight = lol.nextDouble();
        System.out.println("What's your height?");
        double height = lol.nextDouble(); 
        double BMI2 = weight/ (height* height)*10000;
        double BMI = (weight* 703)/ (height* height);
        if(choice.equals("U.S"))
        { 
        NumberFormat internet = NumberFormat.getInstance();
        internet.setMaximumFractionDigits(1);
        System.out.printf("Your BMI is " + internet.format(BMI) + "\n");
        which = 2;
        }
        else if(choice.equals("Standard"))
        {
        NumberFormat internet = NumberFormat.getInstance();
        internet.setMaximumFractionDigits(1);
        System.out.printf("Your BMI is " + internet.format(BMI2) + "\n");
        which = 0;
        }
        else 
        {
            System.out.println("Not an option.");
        }
        if(which==0)
        { 
            int old = 1;
         for(double faf = 0;faf<=BMI2;faf=faf+0.5) 
         {
             double fof = 0;
             fof++;
             if(fof<37 && old!=0)   
             {
              System.out.println("You are underweighted!");
              old = 0;
             }
             else if(fof<50 && old!=0)
             {
              System.out.println("You are Normal Weighted!");
              old = 0;
             }
             else if(fof<60 && old!=0)
             {
              System.out.println("You are Overweighted!");  
              old = 0;
             }
             else if(fof>=60)
             {
              System.out.println("Obesity!");
              old = 0;
             }
         }
        }
         else if(which==2)
         {
             int old = 1;
             for(double faf = 0;faf<=BMI;faf=faf+0.5) 
             {
                 double fof = 0;
                 fof++;

                 if(fof<37 && old!=0)   
                 {
                  System.out.println("You are underweighted!");
                  old = 0;
                 }
                 else if(fof<50 && old!=0)
                 {
                  System.out.println("You are Normal Weighted!");   
                  old = 0;
                 }
                 else if(fof<60 && old!=0)
                 {
                  System.out.println("You are Overweighted!");
                  old = 0;
                 }
                 else if(fof>=60)
                 {
                  System.out.println("Obesity!");
                  old = 0;
                 }
         }

         } 
        lol.close(); 
    }

}

Pardon me if I make any mistakes, English isn't my first language.

Thank you for taking your time to look over my question, I truly appreciate it. -John

Aucun commentaire:

Enregistrer un commentaire