静态方法访问实例变量可改成:import java.io.*;public class Try
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private int n;

static int getNo()throws IOException 
{
System.out.print("Give New Student no : ");
return cin.read();
} public static void main(String[] agro)throws IOException 
{
Try test= new Try();
test.n = test.getNo();
System.out.println(test.n);
}}还可改成:import java.io.*;public class Try
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private String n;

static String getNo()throws IOException 
{
System.out.print("Give New Student no : ");
return cin.readLine();
} public static void main(String[] agro)throws IOException 
{
Try test= new Try();
test.n = test.getNo();
System.out.println(test.n);
}}

解决方案 »

  1.   

    为什么要test.n?不是已经private 了吗?
      

  2.   

    如果我用上面这个代码,输入200,输出却是50,为什么?
    but the second one this correct. why?
      

  3.   

    但是我要的是输入一个interger,不是String,这样还是可以吗?
      

  4.   

    要用test.n这个不是太清楚,应该是因为你在main方法里引用的;
    输入200输出50是因为你上面用的是read()没读完,下面的readline()读完了;
    输入的是interger的话,读取进来后把string转换成interger就是了。
      

  5.   

    import java.io.*;public class Test
    {
    static BufferedReader cin
    = new BufferedReader(new InputStreamReader(System.in));
    private int n;

    public int getNo()throws IOException 
    {
    System.out.print("Give New Student no : ");
    n=Integer.parseInt(cin.readLine());
    return n;
    } public static void main(String[] agro)
    {
    Test test= new Test();
        System.out.println(test.n);
    }}