在 Java 5 中读取输入的最佳实践方法
我正在为孩子们编写一个教程,以学习 Java 5 平台上的编程。
从控制台程序读取文本数据的最佳方法是什么?
我所说的“最佳”是指在 Java 5 级别完成此操作的最常见方法。我意识到从命令行读取文本输入充满了挑战,并且一个受到良好保护的方法必须包含多个级别的异常,这很好。到目前为止,在本教程中,我已经介绍了基本输出、Java 中的基本程序结构以及异常处理。如果您认为在用户输入之前需要涵盖其他内容,我欢迎您提出建议。
有趣的是,对这个问题进行网络搜索并不能立即给出答案,有很多东西需要清除,而且没有最佳实践的感觉。我想这是因为有太多隐藏的警告,它并不像乍看起来那么简单。
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Example {
private BufferedReader br;
private String inputString;
public static void main(String[] args) {
String inputString = null;
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n > ");
try {
inputString = br.readLine();
} catch (IOException ioe) {
System.out.println("\n IOException called.");
}
System.out.println("\n Input was: " + inputString);
}
}
您可以使用以下方式编译它: javac Example.java
使用以下方式运行它: java Example
您是否可以想到更好的方法,这可能对儿童更有启发?有趣的是,这些东西看起来是多么基本,但当你真正观察它时,你会发现这是用最少的错误检查来收集一行文本的大量代码。
如果您想添加一条关于您如何第一次对编程感兴趣的可选评论,我将非常感激。对我来说,这是一次偶然(我的父亲必须带回家一个哑终端才能在家工作,通过拨号连接到unix系统连接到他工作的主机,当他向我展示如何列出“游戏”时,我被迷住了)。
感谢您的帮助。
I'm writing a tutorial for kids, to learn programming on the Java 5 platform.
What's the best way to read in text data from a console program?
By "best" I mean the most common way this would be done at the Java 5 level. I realize reading text input from the command line is fraught with challenges and that a well guarded method would have to incorporate several levels of exceptions, that's fine. At this point in the tutorial, I've covered basic output, basic program structure in Java, exception handling. If you think additional things need to be covered before user input, I'd welcome suggestions.
It's interesting that doing a web search on this question doesn't give an immediate answer, there's a lot to weed through and there's no sense of a best practice. I guess it's because there are so many hidden caveats, it's not as simple as it first seems.
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Example {
private BufferedReader br;
private String inputString;
public static void main(String[] args) {
String inputString = null;
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n > ");
try {
inputString = br.readLine();
} catch (IOException ioe) {
System.out.println("\n IOException called.");
}
System.out.println("\n Input was: " + inputString);
}
}
You would compile this using: javac Example.java
Run it using: java Example
Is there a better way you can think of that might be more instructive for children? Funny how basic these things seem to be, but when you really look at it, that's a lot of code for gathering one line of text with a minimal amount of error checking.
If you want to add an optional comment about how you first got interested in programming, I'd be hugely grateful. For me, it was chance (my father had to bring home a dumb terminal to do work from home, connecting to his work's mainframe over a dial-up connection to a unix system, and when he showed me how to list 'games' I was hooked).
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
扫描仪非常基本,代码也较少。
Scanner is pretty basic and less code.
我个人认为
Scanner
比BufferedReader
更容易一些。这就是我最初学习用户输入的方式。您必须做一些相同的事情,但您可以放弃异常处理并在稍后进行尝试/捕获。
I think a
Scanner
is a bit easier than aBufferedReader
, personally. That's how I learned user input initially.You have to do some of the same things but you can leave off the exception handling and try/catch for a later day.
一些反问句:
孩子们多大了?
您的教育成果目标是什么?或者这主要是为了给他们一些有趣的事情做。
您的 Java 经验水平如何?
我的直觉是,你不应该过度使用“最佳实践”的东西。假设这些是学龄儿童,他们不需要它,他们可能没有能力理解它。因此,对于他们中的大多数人来说,这将是无聊的事情,也是让他们关闭 IT 的好方法。
此外,如果你自己不完全理解“最佳实践”,就很难教授它。在我看来,“最佳实践”是指在大多数情况下最好的技术。为了正确地教授它们,您需要了解(并能够解释)它们为什么有效以及何时不起作用。如果您也了解一些“非最佳实践”的做事方式,这也会有所帮助……这样您就可以解释其中的差异。
(我还想问为什么你将自己限制在 Java 5 上……但这并不是特别相关。)
当我问及“教育成果”时,我的意思是:
这是从事教学工作的人应该问自己的首要问题之一。
话虽如此,我想我会坚持上面的建议。如果你想宣传“最佳实践”,请尝试组织你的材料,以便普通孩子可以轻松跳过“无聊”的部分。
Some rhetorical questions:
What age are the kids?
What are you aiming to achieve in terms of educational outcomes? Or is this mainly about giving them something fun to do.
What is your level of experience with Java?
My gut feeling is that you should not be going overboard with the "best practice" stuff. Assuming that these are school age kids, they don't need it, they probably don't have the outlook to understand it. So for most of them it is going to be boring blah blah, and a good way to turn them off IT.
Besides, it is difficult to teach "best practice" if you don't fully understand it yourself. The way I see it, "best practice" is about techniques that are best most of the time. To teach them properly you need to understand (and be able to explain) why they work, and when they don't work. It also helps if you understand a bit about the "not best practice" ways of doing things too ... so that you can explain the difference.
(I'd also question why you are restricting yourself to Java 5 ... but that's not particularly relevant.)
When I asked about "educational outcomes" I was mean things like:
It is one of the first questions that someone in the business of teaching should be asking themselves.
Having said that, I think I'll stick with my advice above. If you want to beat the drum about "best practice", try to structure your material so that it is easy for the average kid to skip over the "boring" bits.
我的想法是,当孩子们使用 JOptionPane 在新屏幕上看到结果时,他们会更加兴奋。
My idea is that , kids are going to be more exited when they see their result with a new screen using JOptionPane.