Resource leak: ‘input‘ is never closed

在刚开始学习Java的时候,从控制台读取输入的程序可能会有这么一段警告

Resource leak: ‘input’ is never closed
(资源泄漏:“input”从未关闭)
虽然只是warring,但我们要养成好的习惯,在main的末尾加一段代码给它关掉就行了。

       input.close();
import java.util.Scanner;
public class Welcome {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
	   Scanner input = new Scanner(System.in);
       System.out.println("Enter a number for radius:");
       double radius = input.nextDouble();
       double area = radius*radius*3.14159;
       System.out.println("The area for the circle of radius " + radius + " is " +area);
      // input.close();        
	}
  
}

读者可以自行去掉// input.close(); 中的//,就没有这个警告了。

猜你喜欢

转载自blog.csdn.net/weixin_45965358/article/details/113187224