【发布时间】:2016-06-19 15:36:30
【问题描述】:
我一直在以http://www.cs.princeton.edu/courses/archive/spr15/cos126/lectures.html 作为参考自学Java。他们有一个名为 algs4 的库,它有几个类,包括 StdIn,我正在尝试在下面实现。
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Tired
{
public static void main(String[] args)
{
//I thought this while statement will ask for an input
//and if an input is provided, it would spell out each character
while (!StdIn.hasNextChar()) {
StdOut.print(1); //seeing if it gets past the while conditional
char c = StdIn.readChar();
StdOut.print(c);
}
}
}
//This is from StdIn class. It has a method called hasNextChar() as shown below.
/*
public static boolean hasNextChar() {
scanner.useDelimiter(EMPTY_PATTERN);
boolean result = scanner.hasNext();
scanner.useDelimiter(WHITESPACE_PATTERN);
return result;
}
*/
如果我运行代码,它确实会要求输入,但无论我输入什么,都不会发生任何事情,也不会打印出任何内容。
我看到即使StdOut.print(1); 也没有打印出来,所以由于某种原因,它只是卡在while 上
【问题讨论】:
标签: java class input while-loop