如何关闭扫描仪?

时间:2015-09-14 16:32:03

标签: java

我有这个代码可以工作,但我完成后无法关闭扫描仪。 scanner.close()无法在任何地方工作并使用try(Scanner scaner等似乎也无法正常工作。有人能告诉我如何在像我这样的代码中关闭扫描仪吗?

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Random randomGenerator = new Random();
        int input;
        int code = 0;
        int i = 0;
        int[] guesses = new int[7];

        System.out.println("Secretly type the code or input -1 if you want me to                              choose");
        input = scanner.nextInt();
        if (input == -1) {
            code = randomGenerator.nextInt(100);
        }
        else {
            code = input;
        }

        System.out.println("Start guessing!");
        while (i < 7) {
            guesses[i] = scanner.nextInt();

            if (guesses[i] == code) {
                System.out.println("Good guess! You won.");
                System.out.println((i+1) +" guesses");
                i++;



                for (int k=0; k<i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");
                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }

            else if (code < guesses[i] && i != 6) {
                System.out.println("lower");
                i++;
            }

            else if (code > guesses[i] && i != 6) {
                System.out.println("higher");
                i++;
            }

            else {

                System.out.println("No more guesses, you lost");
                System.out.println((i+1) + " guesses");


                for (int k=0; k<=i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");


                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }
        }
    }
} 

4 个答案:

答案 0 :(得分:1)

当您使用另一个流包装流时(就像在扫描仪中一样),关闭流将关闭包裹的流。

这意味着如果关闭扫描仪,则会关闭System.in。

我建议将scanner变量设置为null,并让垃圾收集器将其从堆中删除。除非您明确要关闭程序的输入,否则这可能会产生预期的效果。

答案 1 :(得分:0)

您应该在scanner循环后关闭while。否则你肯定会遇到错误。

答案 2 :(得分:0)

使用

Scanner scanner = ....;
try {
 while () {} ....
} catch (Exception ex) { 
 try {scanner.close();}catch {}  // closes the scanner in case of an exception
} finally { try {scanner.close(); } catch {}}  // makes sure that the scanner closes. try catch because it may fail.

答案 3 :(得分:0)

我无法使用scanner.close()方法重现您的错误,但我认为它不能在循环中运行。这是一个为我工作的例子:

import java.util.Random;
import java.util.Scanner;



public class Test{


public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);
        Random randomGenerator = new Random();
        int input;
        int code = 0;
        int i = 0;
        int[] guesses = new int[7];

        System.out.println("Secretly type the code or input -1 if you want me to                              choose");
        input = scanner.nextInt();
        if (input == -1) {
            code = randomGenerator.nextInt(100);
        }
        else {
            code = input;
        }

        System.out.println("Start guessing!");
        while (i < 7) {
            guesses[i] = scanner.nextInt();

            if (guesses[i] == code) {
                System.out.println("Good guess! You won.");
                System.out.println((i+1) +" guesses");
                i++;



                for (int k=0; k<i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");
                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }

            else if (code < guesses[i] && i != 6) {
                System.out.println("lower");
                i++;
            }

            else if (code > guesses[i] && i != 6) {
                System.out.println("higher");
                i++;
            }

            else {

                System.out.println("No more guesses, you lost");
                System.out.println((i+1) + " guesses");


                for (int k=0; k<=i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");


                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }
        }


        scanner.close();

}
}