在递归函数中使用Scanner类 [英] Use of Scanner class in recursive functions

查看:65
本文介绍了在递归函数中使用Scanner类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用递归方法/函数,该方法/函数使用Scanner类.关闭扫描程序,导致下次在递归中调用同一方法时引发异常.一种解决方法是根本不关闭扫描仪,但这不是正确的方法.我怀疑在递归调用之间使用同一扫描程序对象,因此这就是关闭它会造成严重破坏的原因.如果我的假设是正确的,那么在最后一个方法调用中关闭扫描仪将是一个有效的解决方法(即没有实际的资源泄漏).在进入扫描仪和相关的实现代码之前,我可能还缺少什么吗?

I am trying to use a recursive method/function, which uses the Scanner class. Closing the scanner, causes an exception to be thrown the next time the same method is called within the recursion. A workaround to this is not to close the scanner at all, but this is not a right approach. I suspect the same scanner object is used between recursive calls, so that's why closing it creates havoc. If my assumption is correct then closing the scanner in the last method call would be a valid workaround (i.e. no actual resource leak). Is there anything I may be missing before jumping into Scanner and related implementation code?

编辑

提供的答案确实很有用,而且很有启发性.总而言之,问题在于扫描器不断地重新打开和关闭,而不是递归本身.我避免将扫描器对象作为参数传递的原因是,此示例模拟了一个较大的项目,调用了多个递归函数,因此我必须在所有这些函数中传递扫描器对象.

The answers provided were really useful and enlightening. In summary, the problem is the constant re-opening and closing of the scanner, and not recursion per se. The reason I would avoid passing the scanner object as parameter is that this example simulates a larger project, calling multiple recursive functions and I would have to pass the scanner object in all of them.

从实际的角度来看,从提供的答案来看,我认为仅在上次递归调用中关闭扫描程序即可,而不会造成任何资源泄漏.任何相关的意见都将受到欢迎,尤其是.如果您发现我的方法有问题.

On the practical side, and from the answers provided, I think just closing the scanner in the last recursive call would work without having any resource leaks. Any related opinions would be welcome, esp. if you see something wrong with my approach.

这是我最初的实验的一个例子:

Here is an example of my initial experiment:

package scanner;

import java.util.Scanner;

public class Main {

  public static void acceptValidInput() {

    System.out.print("Enter a number greater than 10: ");
    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();

    // Adding this will make an exception to be thrown:
    sc.close();

    if (i <= 10) {
        acceptValidInput();
    }
}

  public static void main(String[] args) {
    acceptValidInput();
    System.out.println("Your input is valid");
  }
}

推荐答案

一旦您开始使用 Scanner 消耗输入流,就不应再尝试以其他任何方式读取它.换句话说,在构建了一个 Scanner 以便从 System.in 读取之后,您需要将其用于所有从 System.in .这是因为

Once you start to consume an input stream using a Scanner, you should not try to read from it in any other way anymore. In other words, after you have constructed a Scanner to read from System.in, you need to use it for all further reading from System.in. This is because Scanner buffers input, so you have no idea how much input it has already consumed but not emitted yet.

因此,我建议您构造一个 Scanner ,然后将其用于所有读取:

Therefore, I recommend that you construct one Scanner, then use it for all the reading:

public class Main {
  public static void acceptValidInput(Scanner sc) {
    System.out.print("Enter a number greater than 10: ");

    int i = sc.nextInt();

    if (i <= 10) {
      acceptValidInput(sc);
    }
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    acceptValidInput(sc);
    System.out.println("Your input is valid");
    sc.close();
  }
}

这篇关于在递归函数中使用Scanner类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆