当前位置: 代码迷 >> J2SE >> 关于方法的返回值解决办法
  详细解决方案

关于方法的返回值解决办法

热度:61   发布时间:2016-04-24 01:57:27.0
关于方法的返回值
程序如下

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前面去!!
  相关解决方案