Topic: 用if 判断不能实现的问题!

  Print this page

1.用if 判断不能实现的问题! Copy to clipboard
Posted by: drxjava
Posted on: 2004-10-02 07:43

程序如下,要用if判断传递进的年龄项,若是小于零则返回,因为年龄没有小于零的,但是却不能实现,打印结果还是-30,这是为什么呢????
class Person
{
private int age;
void shout()
{
System.out.println("my age is"+age);
}
public void setAge(int x)
{
if(age < 0)
{return;}
else age=x;
}
public int getAge()
{
return age;
}
}
class TestPerson
{
public static void main(String [] args)
{
Person p1=new Person();
Person p2=new Person();
p1.setAge(-30);
p1.getAge ();
p1.shout();
p2.shout();
getSomeone(new Person ());
}
public static void getSomeone(Person p)
{
p.shout();
}
}

2.Re:用if 判断不能实现的问题! [Re: drxjava] Copy to clipboard
Posted by: ljy0000
Posted on: 2004-10-02 08:52

在这里:
public void setAge(int x)
{
if(age < 0)
{return;}
else age=x;
}

应为:
public void setAge(int x)
{
if(x < 0)
{return;}
else age=x;
}

3.Re:用if 判断不能实现的问题! [Re: drxjava] Copy to clipboard
Posted by: drxjava
Posted on: 2004-10-02 16:52

谢谢了呀,原来是我自己的不细心造成的啊!十分感谢呀!:)

4.Re:用if 判断不能实现的问题! [Re: drxjava] Copy to clipboard
Posted by: why
Posted on: 2004-10-03 23:47

drxjava wrote:
谢谢了呀,原来是我自己的不细心造成的啊!十分感谢呀!:)

这岂只不细心
应是粗心大意
切戒!切戒!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923