Java 中作为命令行输入的字符串列表(多行)
我正在尝试为学校做作业,但我不知道如何处理输入。我在下面提供了有关作业上下文的链接:
我对如何完成作业要求的一切,但我不确定如何处理输入。
示例输入是:
a0
0
a00
ab000
给出的输出为:
树 1:
无效!
树 2:
高度:-1
路径长度:0
完整:是
邮购:
树 3:
高度:0
路径长度:0
完整:是
后序:a
树 4:
高度:1
路径长度:1
完整:是
后序:ba
我打算用 Java 来做到这一点。我的问题是,当不在输入文件中进行管道传输时,如何将示例中的多行输入输入到 Windows cmd.exe 行中?因为按 Enter 键只会运行一行输入的程序,而不是创建一个新行。另外,由于作业是自动标记的,因此输入无法交互,那么我如何知道何时停止阅读?
谢谢。
I am trying to do an assignment for school and I don't know how to deal with the input. I have provided a link below for context on the assignment:
I have a general idea on how to do everything the assignment asks, but I'm unsure of how to deal with input.
A sample input is:
a0
0
a00
ab000
Which gives an output of:
Tree 1:
Invalid!
Tree 2:
height: -1
path length: 0
complete: yes
postorder:
Tree 3:
height: 0
path length: 0
complete: yes
postorder: a
Tree 4:
height: 1
path length: 1
complete: yes
postorder: ba
I intend to do this with Java. My question is how do I enter multiple lines of input like in the sample into the Windows cmd.exe line when not piping in an input file? Because pressing enter would just run the program with one line of input instead of making a new line. Also, since the assignment is being marked automatically, the input can't be interactive, so how would I go about knowing when to stop reading?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从任务中:
只需读取 System.in 并写入 System.out。由于输入将通过管道传输到 stdin,因此您将在输入文件末尾得到 EOF。
通过 CMD 窗口交互时,使用 Ctrl-Z 表示 EOF(在 Windows 上)或在 Linux 系统上,使用 Ctrl-D
From the assignment:
Just read System.in and write to System.out. Since the input will be piped to stdin, you will get EOF at the end of the input file.
When interacting via the CMD window, use Ctrl-Z to indicate EOF (on Windows) or on a Linux system, use Ctrl-D
如果您可以使用 System.in,那么您可以使用从 System.in 流读取的 InputStreamReader。然后,使用 BufferedReader 使用 readLine() 方法获取每一行。例如,看看这段代码:
If you can use System.in, then you could use an InputStreamReader that reads from a stream of System.in. Then, use a BufferedReader to get each line using readLine() method. For example, look at this code:
这段代码将毫无问题地工作 -
This code will work without any problem -