0

I am coding in Java and using TextPad editor.

I am trying to write code that sets "r" equal to a certain value depending on whether the user is male or female. I have already asked the user to put "1" if they are male and "2" if they are female.

I have set r as a double. D, and weight have been defined by the user earlier in the code.

Here is the code that I am trying to use and it keeps giving me an error saying that I have not defined "r".

if (gender == 1) r = 0.73D; if (gender == 2) r = 0.66D; else System.out.println("Please enter 1 or 2 for male or female."); alcoholAbsored = (3.701*D)/(weight*r); 

All of the code compiles until I get to the formula for alcoholAbsorbed and then tells me r is not defined. Technically there is no problem with the if/else statement when I compile but then when I try to use r there becomes a problem. I've ended up using this code for now but this is not really what I want because what if the user puts something other than 1 or 2?

if (gender == 1) r = 0.73D; else r = 0.66D; alcohol absorbed = (3.701*D)/(weight*r); 

Can someone please tell me what I am doing wrong and how I might go about fixing it? Please and thank you!

5
  • 1
    java !== javascript Commented Sep 21, 2016 at 3:16
  • declare r above the ifs statements... var r; Commented Sep 21, 2016 at 3:17
  • Declare r globally. And initialise it with some value Commented Sep 21, 2016 at 3:22
  • Actually in your previous code the else part does not initialises the r. Thats why you are getting that error Commented Sep 21, 2016 at 3:25
  • But the value of r changes depending on whether the user enters 1 or 2 for male or female. r is not a static value. Commented Sep 21, 2016 at 3:26

2 Answers 2

1

The problem with your code is that in the first version, if it goes to the else block (which it will even if the user has entered 1) then r won't be initialized so at the next line it cannot find any value for it. The correct code would be something like this:

if (gender == 1) r = 0.73D; else if (gender == 2) r = 0.66D; else { r = -1D; System.out.println("Please enter 1 or 2 for male or female."); } if ( r != -1D) alcoholAbsored = (3.701*D)/(weight*r); 

Or:

if (gender == 1 || gender == 2){ if (gender == 1) r = 0.73D; else r = 0.66D; alcoholAbsored = (3.701*D)/(weight*r); } else System.out.println("Please enter 1 or 2 for male or female."); 
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to put the alcoholAbsorbed formula in an if-statement?
No, but you just need to make sure that r has been initialized in all the possible branches that reach to alcoholAbsorbed.
1

Its better to code like this

if(gender==1 || gender==2) { if (gender == 1) r = 0.73D; else if (gender == 2) r = 0.66D; alcoholAbsored = (3.701*D)/(weight*r); // do something } else { System.out.println("Please enter 1 or 2 for male or female."); } 

1 Comment

Hey TNT. I am posting it from my mobile. If the code is understandable. I think there is no need to shifting curly braces and scope statements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.