Java 打印局部变量的值
我无法打印j值 您需要返回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
,因此您的函数应该如下所示:
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;
}