程序如下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的值显示出来呢?

解决方案 »

  1.   


    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));}
    }
    这样试试
      

  2.   

    谢谢1楼,能显示出值来,我想知道当System....在方法中(有返回值的),为什么会不行
      

  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前面去!!
      

  4.   

    你方法中的输出写在了return后面,return执行后你那个输出语句就不会被执行了,所以会报错!