//VariableScope.java public class VariableScope { public static void main(Sring args[]) { int x; x=10; if (x==10) { int y=20; System.out.println("x="+x); System.out.println("y="+y); } //在这里y就没有了
//VariableScope.java public class VariableScope { public static void main(Sring args[]) { int x; int y; x=10; if (x==10) { y=20; System.out.println("x="+x); System.out.println("y="+y); } System.out.println("x="+x); System.out.println("y="+y); } } 就这样了
sorryy要赋初值//VariableScope.java public class VariableScope { public static void main(Sring args[]) { int x; int y=0; x=10; if (x==10) { y=20; System.out.println("x="+x); System.out.println("y="+y); } System.out.println("x="+x); System.out.println("y="+y); } }
to: anjun(我思故我在!) 我刚才照你的编译了一下,提示Public class VariableScoppe must be defined in a file called"VariableScope.java" 这是什么意思啊?楼上各位谢谢了
public class VariableScope
{
public static void main(Sring args[])
{
int x;
x=10;
if (x==10)
{
int y=20;
System.out.println("x="+x);
System.out.println("y="+y);
}
//在这里y就没有了
System.out.println("x="+x);
System.out.println("y="+y);
}
}sorry,y是因为作用域的问题
if (x==10)
{
int y=20;
System.out.println("x="+x);
System.out.println("y="+y);
}
出来就不认识拉~~string 拼错了!
public class VariableScope
{
public static void main(Sring args[])
{
int x;
int y;
x=10;
if (x==10)
{
y=20;
System.out.println("x="+x);
System.out.println("y="+y);
}
System.out.println("x="+x);
System.out.println("y="+y);
}
}
就这样了
public class VariableScope
{
public static void main(Sring args[])
{
int x;
int y=0;
x=10;
if (x==10)
{
y=20;
System.out.println("x="+x);
System.out.println("y="+y);
}
System.out.println("x="+x);
System.out.println("y="+y);
}
}
anjun(我思故我在!)
我刚才照你的编译了一下,提示Public class VariableScoppe must be defined in a file called"VariableScope.java"
这是什么意思啊?楼上各位谢谢了
我以前学过c,pascal和汇编,现在学java,听着老师讲课,对他所说的类特别不明白,我总感觉有点象c中的子函数。高手能不能给解释一下类的概念啊,最好再写个例子。我先谢谢了
continue learning, baby
第二个错误:你的y 是在if(){}内定义的.
在if(){}外部使用y,超过了他的使用域,System.out.println("y="+y);所以出错
if (x==10)
{
int y=20;
System.out.println("x="+x);
System.out.println("y="+y);
}
超出这个范围,y就不可见了.
{
public static void main(String[] args)
{
int x;
x=10;
int y=20;
if (x==10)
{ System.out.println("x="+x);
System.out.println("y="+y);
}
System.out.println("x="+x);
System.out.println("y="+y);
}
}
建议你用JB开好java,这样会对你有帮助
好运与你同行。。