Help with operator

Pis7ftw

New member
Joined
Aug 13, 2016
Messages
3
Programming Experience
Beginner
I'm new to C# and I'm having trouble with the OR operator. I've watched several tutorials and haven't figured out what's wrong yet.

C#:
        public static string AgeArticleModifier(int age, string ageUnit)        {
            string strAgeArticle;


            if (age == 8 || age == 18 || age >= 80 || age <= 89)
            {
                strAgeArticle = "an";
                MessageBox.Show("Patient is " + strAgeArticle + " " + age + " " + " year old");
                return strAgeArticle;
            }
            else
            {
                strAgeArticle = "a";
                MessageBox.Show("Patient is " + strAgeArticle + " " + age + " " + " year old");
                return strAgeArticle;
            }            
        }

Essentially, if the age is 8 or between 80 or 89, it's grammatically correct to say "an 8 year old" or "an 85 year old". Otherwise the article would be "a" as in "a 3 year old" or "a 10 year old". The whole purpose of this method is to compensate for that. However, the MessageBox will always show "Patient is AN 49 year old" regardless of the actual age found in int age. The condition is obviously not working here.

Can someone point me in the right direction?

THank you!
 
Essentially, if the age is 8 or between 80 or 89

Your problem is right there. That is NOT a correct description. What you want is to determine whether the age is 8 OR between 80 AND 89. This:
if (age == 8 || age == 18 || age >= 80 || age <= 89)
should be this:
if (age == 8 || age == 18 || (age >= 80 && age <= 89))
 
Thanks!!! I had a feeling it was something simple. I'll be taking the lesson here to heart in hopes of not embarassing myself in the future!
 
Back
Top Bottom