public class Yichang1
{
public static void main(String [] args)
{
try
{
int x=System.in.read();
}catch(Exception e){}
while(x!='q')
{
System.out.println((char)x);
try
{
x=System.in.read();
}catch(Exception e){}
}
}
}错误信息:Yichang1.java:9: cannot resolve symbol
symbol  : variable x
location: class Yichang1
                while(x!='q')
                      ^
Yichang1.java:11: cannot resolve symbol
symbol  : variable x
location: class Yichang1
                        System.out.println((char)x);
                                                 ^
Yichang1.java:14: cannot resolve symbol
symbol  : variable x
location: class Yichang1
                                x=System.in.read();
                                ^
3 errors

解决方案 »

  1.   

     int x = 0;定义在try的上面.
      

  2.   


    谢谢 ,可以了,但是我还是不明白,之前我还是在外面定义了x,即:int x;  就是没赋值,怎么就有错呢?
    下面代码能正确运行了。
    public class Yichang1
    {
    public static void main(String [] args)
    {
    int x=0;
    try
    {
    x=System.in.read();
    }catch(Exception e){}
    while(x!='q')
    {
    System.out.println((char)x);
    try
    {
    x=System.in.read();
    }catch(Exception e){}
    }
    }
    }
      

  3.   

    java 与c不一样,一个变量在使用前必须给赋值,这下没问题了吧