Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印局部变量的值_Java - Fatal编程技术网

Java 打印局部变量的值

Java 打印局部变量的值,java,Java,我无法打印j值 您需要返回j,因此您的函数应该如下所示: package Student; public class Example { int i = 10;//instance variable which is created under class void age()// age is method here { int j=11;// local variable which is created under method }

我无法打印j值

您需要返回
j
,因此您的函数应该如下所示:

package Student;

public class Example
{
    int i = 10;//instance variable which is created under class

    void age()// age is method here
    {   
        int j=11;// local variable which is created under method

}

    public static void main(String[] args) {


        Example ex = new Example();//Creating the object 
        System.out.println("Display the valule" ex.age());


        ex.age();

    }
}
System.out.println(“显示值”+ex.age())

您希望上面的行打印年龄,但不会,因为您的年龄方法不返回任何内容

试试这个:

int age()// age is method here
{   
        int j=11;
        return j; // return j
}

sysout将打印在中传递的任何值。方法age()未返回任何值。应该修改age()方法,使其返回类型为int,并返回变量j,而不仅仅是设置值。您通常应该使用如下所示的getter和setter,调用set方法并使用get方法在sysout中打印

int age()// Now your age method will return an integer value
{   
        int j=11;// local variable which is created under method
        return j;
}

Java教程:您需要从
age
返回
j
。另外,当你问的时候,你需要明确地说出你有什么问题。这是一个非常模糊的问题,你需要在sysout@Isac谢谢你指出
void setAge() {
 j = 11;
}

int getAge() {
   return j;
}