我是编程的新手,我想问为什么在我的代码中我不需要在构造函数和方法中使用return函数?
另外为什么在使用yearPasses函数之后,年龄增加3而不是1?
为漫长的代码道歉
public class Person
{
private int age;
public Person(int initialAge)
{
// Add some more code to run some checks on initialAge
if (initialAge<0)
{
System.out.println("Age is not valid, setting age to 0.");
initialAge = 0;
age = initialAge;
}
else
{
age = initialAge;
}
}
public void amIOld()
{
if (age<13)
{
System.out.println("You are young.");
}
else if (age>=13 && age<18)
{
System.out.println("You are a teenager.");
}
else
{
System.out.println("You are old.");
}
}
public …Run Code Online (Sandbox Code Playgroud) java ×1