如何使StdIn.isEmpty()返回true? [英] how to make StdIn.isEmpty() return true?

查看:384
本文介绍了如何使StdIn.isEmpty()返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用算法Coursera算法课程中提供的普林斯顿库中的StdIn.isEmpty()方法,但对其工作方式感到困惑.我有声明

I am using the StdIn.isEmpty() method from the Princeton libraries provided in the Algorithms Coursera algorithms course but am confused as to how it works. I have the statement

while (!StdIn.isEmpty())

附带一些读取用户输入的代码,但我似乎无法打破循环.根据我的理解,如果我在不输入任何文本的情况下按回车键,则意味着标准输入为空,而while循环应被中断.我查找了isEmpty的代码,但未澄清任何内容:

with some code enclosed that reads user input but I can't seem to break out of the loop. By my understanding if I hit enter without typing in any text that should mean that standard input is empty and the while loop should be broken. I looked up the code for isEmpty but it did not clarify anything:

public static boolean isEmpty() {
    return !scanner.hasNext();
}

有人可以澄清标准输入的工作原理,并帮助我解决对这种方法的误解吗?

Can someone clarify how standard input works and help me fix my misunderstanding of this method?

推荐答案

在读取每个数字之前,程序使用StdIn.isEmpty()方法检查输入流中是否还有其他数字.我们如何发出信号说我们没有更多数据要输入?按照惯例,我们键入一个特殊的字符序列,称为文件结尾序列.不幸的是,我们在现代操作系统上通常遇到的终端应用程序对于这个至关重要的序列使用不同的约定.在本书中,我们使用Ctrl-D ... 计算机科学Sedgewick

Before reading each number, the program uses the method StdIn.isEmpty() to check whether there are any more numbers in the input stream. How do we signal that we have no more data to type? By convention, we type a special sequence of characters known as the end-of-file sequence. Unfortunately, the terminal applications that we typically encounter on modern operating systems use different conventions for this critically important sequence. In this book, we use Ctrl-D... Computer Science Sedgewick

因此isEmpty实际上是isCtrl-D.

此外,请在终端应用程序中尝试StdIn.isEmpty(),不做任何事,这将使您没有布尔值,而是标准输入流请求输入.因此,当以下程序运行时:

Also, try StdIn.isEmpty() in the terminal application without anything beforehand will leave you with no boolean but a standard input stream requesting inputs. So when a program like the following one runs:

public class Average
  {
     public static void main(String[] args)
     {  // Average the numbers on standard input.
        double sum = 0.0;
        int n = 0;
        while (!StdIn.isEmpty())
        {  // Read a number from standard input and add to sum.
           double value = StdIn.readDouble();
           sum += value;
           n++;
        }
        double average = sum / n;
        StdOut.println("Average is " + average);
     }
 } 

,当第一次确定条件while (!StdIn.isEmpty)而不是StdIn.readDouble()时弹出标准输入流.如果您随后键入一些数字,然后将它们返回,导致标准输入流为非空,则程序将在while主体内跳转.我通过测试

, the standard input stream pops out when decide the conditional while (!StdIn.isEmpty) for the first time instead of StdIn.readDouble(). If you then type in some numbers, return them, causing standard input stream to be non-empty, the program would then jump inside the while body. I found out this is the case by testing

> java Average
 [DrJava Input Box] (And I hit Ctrl+D here)
Average is NaN

NaN表示n = 0,这意味着当身体不被触摸时.

NaN means n = 0 which means the while body was left no touched.

这篇关于如何使StdIn.isEmpty()返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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