Jump to content

Hi this is my code and I was trying to make it so that it asks for the gender, for some reason it just skips the question and goes straight to the calculation. Any suggestions? Thanks for the help in advance.

 

import java.util.Scanner;

public class BMR
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        double Height;
        double Weight;
        int Age;
        
        System.out.println("Hello, please allow me some information so that I can figure out how much chocolate bars you would need to eat to keep weight.");
        System.out.println("Height in inches:");
        Height = keyboard.nextDouble();
        System.out.println("Weight in pounds:");
        Weight = keyboard.nextDouble();
        System.out.println("Age:");
        Age = keyboard.nextInt();
        System.out.println("Male or Female");
        String Gender;
        Gender = keyboard.next();
        
        double Cocobars;
        double BMRMale;
        if (Gender.equals("male")){
            BMRMale = 66 + (6.23 * Weight) + (12.7 * Height) - (6.8 * Age);
            Cocobars = (BMRMale / 230);
            System.out.println("Your BMR is "+BMRMale+" calories so you would need to eat "+Cocobars+" chocolate bars.");
        }else{
            double BMRFemale;
            BMRFemale = 655 + ( 4.35 * Weight ) + (4.7 * Height ) - (4.7 * Age );
            Cocobars = (BMRFemale / 230);
            System.out.println("Your BMR is "+BMRFemale+" calories so you would need to eat "+Cocobars+" chocolate bars.");
        }
    }    
}

Link to comment
https://linustechtips.com/topic/541325-java-help/
Share on other sites

Link to post
Share on other sites

Seems like it should work.

 

BUt whats with the lack of methods???

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167560
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

? Please explain, it just skips the male and female part and goes straight to the BMR part.

 

does it ask for age, weight, and all that stuff?

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167579
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

Yes it ask for each but skips the gender part.

Odd, it seems like it shouldent.

 

That said, I see a bunch of other problems with your code, not problems that would stop it from working, but problems.

 

  • Lack of driver/different classes
  • Lack of methods
  • user must input male exactly, otherwise the program will think they are female. If they write Male or Man or something, it will think they are female
  • Variables are declared halfway through the code

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167603
Share on other sites

Link to post
Share on other sites

2 minutes ago, spwath said:

Odd, it seems like it shouldent.

 

That said, I see a bunch of other problems with your code, not problems that would stop it from working, but problems.

 

  • Lack of driver/different classes
  • Lack of methods
  • user must input male exactly, otherwise the program will think they are female. If they write Male or Man or something, it will think they are female
  • Variables are declared halfway through the code

Oh, what do you mean by lack of methods?

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167614
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

Oh, what do you mean by lack of methods?

How much programing experience do you have?

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167621
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

We have brought up methods in class before, this my 3rd week in the course. Got it to work, thank you :)

What did you have to do?

It looked fine to Me.

 

 

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167657
Share on other sites

Link to post
Share on other sites

15 minutes ago, spwath said:

What did you have to do?

It looked fine to Me.

 

 

Yeah it just ran normally this time.. For checking if a number is odd I would just make sure I could divide it by 2 and it doesn't leave decimals correct? 

Also when you said declare variables, do you mean at the beginning? 

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167702
Share on other sites

Link to post
Share on other sites

7 minutes ago, Sebastian Kurpiel said:

Also is there a simple way to round to 2 decimals?

No

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167856
Share on other sites

Link to post
Share on other sites

35 minutes ago, Sebastian Kurpiel said:

Yeah it just ran normally this time.. For checking if a number is odd I would just make sure I could divide it by 2 and it doesn't leave decimals correct? 

Also when you said declare variables, do you mean at the beginning? 

If(num%2==0)

Num is even

Else

Num is odd

n0ah1897, on 05 Mar 2014 - 2:08 PM, said:  "Computers are like girls. It's whats in the inside that matters.  I don't know about you, but I like my girls like I like my cases. Just as beautiful on the inside as the outside."

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167862
Share on other sites

Link to post
Share on other sites

2 minutes ago, elpiop said:

multiply the number by 100, then round it using Math.round, and then divide it by 100. 

or you can use DecimalFormat, which would be pretty easy also. 

Could you write the code for that?

21 minutes ago, spwath said:

If(num%2==0)

Num is even

Else

Num is odd

Thank you, you are the man my friend.

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7167987
Share on other sites

Link to post
Share on other sites

57 minutes ago, Sebastian Kurpiel said:

Sorry, how would add that to code? Like would I add it to the beginning?

That code snippet will simply take a number which is in a variable, round it, and put it in another variable. 

Therefore, you can add that code wherever you want, as long as the number to round is in a variable. 

 

For example:

Cocobars = (BMRMale / 230);
Cocobars = Math.round(Cocobars * 100) / 100;

Now the Cocobars number is rounded to two decimal places. 

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7168361
Share on other sites

Link to post
Share on other sites

11 hours ago, Philosobyte said:

That code snippet will simply take a number which is in a variable, round it, and put it in another variable. 

Therefore, you can add that code wherever you want, as long as the number to round is in a variable. 

 

For example:


Cocobars = (BMRMale / 230);
Cocobars = Math.round(Cocobars * 100) / 100;

Now the Cocobars number is rounded to two decimal places. 

thank you :) my code looks more simple now.

Link to comment
https://linustechtips.com/topic/541325-java-help/#findComment-7170578
Share on other sites

Link to post
Share on other sites

  • 5 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×