gpt4 book ai didi

java - 资源泄漏 conIn 从未关闭

转载 作者:行者123 更新时间:2023-11-30 07:34:30 25 4
gpt4 key购买 nike

这段代码给我带来了资源泄漏:

conIn never closed

为什么会发生这种情况以及如何解决它?

package homeWork;
import java.util.*;

public class MainClass
{

public static void main(String[] args)
{
Scanner conIn = new Scanner (System.in);
ShoppingBag sb = new ShoppingBag(0.06f);

int count = 0;
float cost = 0.0f;

System.out.print("Enter count (use 0 to stop): ");

count = conIn.nextInt();
while (count < 0);
{
System.out.print("Enter Cost");
cost = conIn.nextFloat();
sb.place(count, cost);

System.out.print("Enter count (use 0 to stop): ");
count = conIn.nextInt();
System.out.print(sb);

}
}
}

最佳答案

您没有在 Scanner 对象上调用 conIn.close();。 (docs)

关于java - 资源泄漏 conIn 从未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com