小编Nie*_*den的帖子

扫描仪永远不会关闭

我正在开发一款游戏,我的扫描仪遇到了一些问题.我的资源泄漏扫描程序从未关闭过.

但我以为我的扫描仪在没有关闭它之前就已经工作了.但现在不是.有人可以帮帮我吗?

import java.util.Scanner;

public class Main {

    public static final boolean CHEAT = true;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int amountOfPlayers;
        do {
            System.out.print("Select the amount of players (1/2): ");
            while (!scanner.hasNextInt()) {
                System.out.println("That's not a number!");
                scanner.next(); // this is important!
        }

        amountOfPlayers = scanner.nextInt();
        while ((amountOfPlayers <= 0) || (amountOfPlayers > 2));
        System.out.println("You've selected " + amountOfPlayers+" player(s)."); 
    }
}
Run Code Online (Sandbox Code Playgroud)

java resource-leak java.util.scanner

26
推荐指数
2
解决办法
16万
查看次数

标签 统计

java ×1

java.util.scanner ×1

resource-leak ×1