eclipse中java出现资源泄漏错误

问题描述 投票:0回答:2

资源泄漏:“sc”未关闭。

“sc”未关闭是什么意思?

public static void main(String atr[]){
    Scanner sc = new Scanner(System.in);
    int a=sc.nextInt();
    int sum=0;
    while(a<0){
        int r=a%10;
        a=a/10;
        sum = sum + r*r*r;
    }
}
java android eclipse memory-leaks
2个回答
0
投票

它希望您关闭扫描仪。当我将它放入代码中时,它并没有阻止它运行,只是给出了一条警告消息。如果您想消除错误,只需简单地输入

sc.close();

在代码末尾,警告应该消失。


0
投票

请尝试下面的代码

static Scanner sc ;
public static void main(String atr[]){
  try{
    sc = new Scanner(System.in);
    int a=sc.nextInt();
    int sum=0;
        while(a<0){
            int r=a%10;
            a=a/10;
            sum = sum + r*r*r;
    }
    }catch(Exception e){
        e.printstacktrace();
    }finally{
        if(sc != null){
        sc.close()
        }
    }
}

完成后您必须关闭扫描仪对象:)

© www.soinside.com 2019 - 2024. All rights reserved.