【问题标题】:terminating input at the end of input in java在java中的输入末尾终止输入
【发布时间】:2016-04-20 16:40:03
【问题描述】:

如何在 codeforces.com(http://codeforces.com/problemset/problem/661/C) 中不输入任何 EOF 字符的情况下终止扫描程序。 在输入的每一行中给出一个整数,然后在 n 行之后 (n 不是问题的指定数字)输入没有任何新行。所以给一些方法来确定输入中没有更多的行!

【问题讨论】:

  • 我想读超时?如何手动输入 STDIN?

标签: java input java.util.scanner eof


【解决方案1】:

在您发布的示例中,您肯定可以定义一个超时计时器来关闭扫描仪,但我认为对于初学者级别来说,更简单的方法可以做到这一点,例如在输入为空时进行捕捉(这是一些如何输入一个EOF字符太恕我直言)

示例:

public static void main(String[] args) {
    System.out.println("hi type some numbers, but just enter to finish");
    Scanner scan = new Scanner(System.in);
    String c = scan.nextLine();
    while (!c.equalsIgnoreCase("")) {
        System.out.println("nice, try again ");
        c = scan.nextLine();
    }
    System.out.println("ok bye");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 2019-08-24
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多