C
C#2y ago
shawski.

✅ need help with a problem

public Person(int initialAge)
{
// Add some more code to run some checks on initialAge
if (initialAge < 0)
{
Console.WriteLine("Age is not valid, setting age to 0.");
age = 0;
}
initialAge = age;

}
public void amIOld()
{
// Do some computations in here and print out the correct statement to the console
List<int> ages = new List<int>();
ages.Add(age);

foreach (int i in ages)
{
if (i < 13)
{
Console.WriteLine("You are young.");
}
else if (i >= 13 && i < 18)
{
Console.WriteLine("You are a teenager.");
}
else
{
Console.WriteLine("You are old.");
}
}
}
public Person(int initialAge)
{
// Add some more code to run some checks on initialAge
if (initialAge < 0)
{
Console.WriteLine("Age is not valid, setting age to 0.");
age = 0;
}
initialAge = age;

}
public void amIOld()
{
// Do some computations in here and print out the correct statement to the console
List<int> ages = new List<int>();
ages.Add(age);

foreach (int i in ages)
{
if (i < 13)
{
Console.WriteLine("You are young.");
}
else if (i >= 13 && i < 18)
{
Console.WriteLine("You are a teenager.");
}
else
{
Console.WriteLine("You are old.");
}
}
}
why does it print "you are young" no matter what the input is?
20 Replies
Anton
Anton2y ago
ages.Add(age) what's the age here? age seems to never be assigned, so it's always zero
shawski.
shawski.OP2y ago
thats supposed to add the user input to a list unless i did it wrong
Anton
Anton2y ago
what's the point of doing the list? just check the age
shawski.
shawski.OP2y ago
yeah you're right im not sure why i did that gimme a sec i believe i know what i did wrong ill lyk if it works alright i got it working i assigned age wrong and messed up the initial check all's good now though
Accord
Accord2y ago
Closed!
Anton
Anton2y ago
don't name your member variables like your locals btw prefixing with an underscore is best practice
shawski.
shawski.OP2y ago
i didnt name any of those. it was a practice problem i was trying to solve. wdym?
Anton
Anton2y ago
_age if it's a private field
shawski.
shawski.OP2y ago
alright
Anton
Anton2y ago
public fields aren't usually used at all
shawski.
shawski.OP2y ago
good to know. my job was just to write the methods
Anton
Anton2y ago
I personally name them like properties when I do use them
shawski.
shawski.OP2y ago
so i should do this instead?
public int _age;
public int _age;
if i were to write the whole thing myself
Anton
Anton2y ago
private i just told you you don't typically use public fields
shawski.
shawski.OP2y ago
i said before, i didnt write this. i just wrote inside the method, not around it.
Anton
Anton2y ago
bruh you just typed above "if i were to write the whole thing myself"
shawski.
shawski.OP2y ago
yeah i forgot i said that so why should i add _ to age im curious is it just conventionally how private fields are named?
Anton
Anton2y ago
mostly to visually distinguish them from local variables
shawski.
shawski.OP2y ago
alright thanks for that tip
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server