日期:2014-05-20  浏览次数:20657 次

关于方法的返回值
程序如下

class TestStuff
{
public int takeTwo(int x, int y)
{
//x = 0; y = 3;
int z = x > y ? x : y;
return z;
//System.out.println("Max is " + z);
}

public static void main(String[] args)
{

TestStuff t = new TestStuff();
t.takeTwo(12,3);
}
}

以上能运行,但没有值,当保留"System.out.println("Max is " + z);"这句时,提示此句是不可运行代码
我要怎样才行将Z的值显示出来呢?

------解决方案--------------------
Java code

class TestStuff
{
public int takeTwo(int x, int y)
{
//x = 0; y = 3;
int z = x > y ? x : y;
return z;
//System.out.println("Max is " + z);
}

public static void main(String[] args)
{

TestStuff t = new TestStuff();
System.out.println(t.takeTwo(12,3));

}
}

------解决方案--------------------
public int takeTwo(int x, int y)
{
//x = 0; y = 3;
int z = x > y ? x : y;
return z;
//System.out.println("Max is " + z);
}

System.out.println("Max is " + z); 这句话必须放到return前面去!!