Java - 如何从用户获取不确定数量的输入

发布于 2024-10-02 14:38:24 字数 444 浏览 4 评论 0原文

Java 问题:

我想从用户处获取不确定行数的输入。例如,我希望用户继续以字符串形式输入人名,直到用户没有其他名字可输入为止。我想要一种用户友好的方式来轻松指示用户没有更多输入。该循环可能如下所示。有什么好主意吗?任何有关改进代码片段的提示也将受到赞赏。

 BufferedReader read = new BufferedReader(new InputStreamReader(System.in));     

 while( <user still has more input> ) {
      System.out.println("Enter name: ");
      String s = read.readLine();
      ...
      <do something with s>;
      ...
 }

谢谢你!

Java Question:

I want to take an undetermined number of lines of input from a user. For instance, I want the user to continue entering names of people as strings until the user has no other names to enter. I want a user-friendly way to easily indicate that the user has no more input. The loop may look like the following. Any good ideas? Any tips on improving the snippet of code is appreciated as well.

 BufferedReader read = new BufferedReader(new InputStreamReader(System.in));     

 while( <user still has more input> ) {
      System.out.println("Enter name: ");
      String s = read.readLine();
      ...
      <do something with s>;
      ...
 }

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不…忘初心 2024-10-09 14:38:24

您需要一个不是名称但指示不再有数据的值。空行就好了。您还想查询他们是否真的想退出,这样万一他们不小心按回车键,他们也不会丢失所有数据。

name = read.readLine(); 
Pattern p = Pattern.compile("^\\s$"); //empty line or one of just space chareacters
if (p.matcher(name).matches()) {
       System.out.println("Are you done?[y/n]");
       if (Pattern.matches("[yY]", read.readLine()) {
          //quit your loop
       }
}

You need a value that isn't a name but indicates no more data. An empty line would be good. You also want to query if they really want to quit, so in case they accidentally hit return they haven't lost all their data.

name = read.readLine(); 
Pattern p = Pattern.compile("^\\s$"); //empty line or one of just space chareacters
if (p.matcher(name).matches()) {
       System.out.println("Are you done?[y/n]");
       if (Pattern.matches("[yY]", read.readLine()) {
          //quit your loop
       }
}
亚希 2024-10-09 14:38:24

您可以有一个布尔变量,例如 finished,并检查用户是否输入 'Q' 或其他项目来指示他们已完成。如果是这样,请将 finished 设置为 true,您将在下一次迭代时退出循环。

这是一个简单的示例,

boolean finished = false;
String name = null;  // initialized to a default value.

System.out.println("Enter 'Q' to quit");
while(!finished) {
System.out.println("Enter name: ");
name = read.readLine();  // moved declaration outside of loop
if(name.equals("Q")) { 
   finished = true;
 }
  // do work with name
}

我还对您的代码进行了一些修改,

  • 我向用户添加了一条消息以指示终止输入的内容。
  • 我已将您的变量 s 重命名为更有意义的名称,因为您正在存储用户的name,因此将其更改为 name 似乎是合理的。
  • 由于习惯的原因,我还使用默认值初始化了变量,因此我将 name 变量分配给 null。并非完全必要,但我认为这是一个很好的做法。
  • 由于我已经为 name 指定了默认值,因此我已将其移到循环之外。

You could have a boolean variable, such as finished, and check if the user enters 'Q' or another item to indicate that they are finished. If so, set finished to true and you will exit your loop on the next iteration.

Here is a quick example,

boolean finished = false;
String name = null;  // initialized to a default value.

System.out.println("Enter 'Q' to quit");
while(!finished) {
System.out.println("Enter name: ");
name = read.readLine();  // moved declaration outside of loop
if(name.equals("Q")) { 
   finished = true;
 }
  // do work with name
}

I have also modified your code a bit,

  • I have added a message to the user to indicate what terminates the input.
  • I have renamed your variable s to something more meaningful, because you are storing the user's name it seemed reasonable to change it to name.
  • As a force of habit, I have also initialized my variables with default values, so I assigned the name variable to null. Not totally necessary, but I consider it good practice.
  • Since I have assigned name a default value, I have moved it outside of your loop.
眼藏柔 2024-10-09 14:38:24

一个简单的方法是让用户在完成后输入一个空行。您只需检查是否 line.equals("") 或者之前修剪它。

A simple way is to have users enter an empty line when they are done. You'd just check if line.equals("") or maybe trim it before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文