// reads the player's lottery ticket from the console. Collect 6
// unique numbers. Little bug here.
public static Set<Integer> getTicket() {
Set<Integer> ticket = new TreeSet<Integer>();
try (Scanner console = new Scanner(System.in)) {
System.out.print("\nType your " + NUMBERS + " unique lotto numbers: ");
while (ticket.size() < NUMBERS) {
int number = console.nextInt();
ticket.add(number);
}
}
return ticket;
}
Eclipse仍在向我发出警告“资源泄漏:'控制台永远不会关闭'。确实没有console.close()。但我读过Scanner(System.in)是自动关闭的,暗示我不应该得到这个警告。
所以有几种可能性:
感谢您的评论。