【问题标题】:I have to make a loop taking a users input until "done" is entered我必须制作一个循环来接受用户输入,直到输入“完成”
【发布时间】:2014-01-02 13:26:56
【问题描述】:

我正在尝试创建一个ArrayList,它接受用户输入的多个名称,直到插入单词done,但我不确定如何。如何实现?

【问题讨论】:

  • 您正在使用任何特定的编程语言?无论如何,至少发布一些您尝试的代码,否则这个问题将因缺乏努力而关闭。
  • 很抱歉忘记添加它的java
  • 您是否希望在列表中插入“完成”?你做了什么代码来激发这个问题的解决方案?

标签: java string loops arraylist


【解决方案1】:
    ArrayList<String> list = new ArrayList<String>();
    String input = null;
    while (!"done".equals(input)) {
        //  prompt the user to enter an input
        System.out.print("Enter input: ");

        //  open up standard input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


        //  read the input from the command-line; need to use try/catch with the
        //  readLine() method
        try {
            input = br.readLine();
        } catch (IOException ioe) {
            System.out.println("IO error trying to read input!");
            System.exit(1);
        }
        if (!"done".equals(input) && !"".equals(input))
            list.add(input);
    }
    System.out.println("list = " + list);

【讨论】:

  • 虽然正确,但通常首选 input.equals("done") 或使用变量来保存中断字符串。我还可以建议将两者都转换为大写,以避免区分大小写问题吗? (编辑:事实上,我刚刚做出了改变......)
【解决方案2】:

我可能会这样做 -

public static void main(String[] args) {
  System.out.println("Please enter names seperated by newline, or done to stop");
  Scanner scanner = new Scanner(System.in);     // Use a Scanner.
  List<String> al = new ArrayList<String>();    // The list of names (String(s)).
  String word;                                  // The current line.
  while (scanner.hasNextLine()) {               // make sure there is a line.
    word = scanner.nextLine();                  // get the line.
    if (word != null) {                         // make sure it isn't null.
      word = word.trim();                       // trim it.
      if (word.equalsIgnoreCase("done")) {      // check for done.
        break;                                  // End on "done".
      }
      al.add(word);                             // Add the line to the list.
    } else {
      break;                                    // End on null.
    }
  }
  System.out.println("The list contains - ");   // Print the list.
  for (String str : al) {                       // line
    System.out.println(str);                    // by line.
  }
}

【讨论】:

    【解决方案3】:
    String[] inputArray = new String[0];
    do{
      String input=getinput();//replace with custom input code
      newInputArray=new String[inputArray.length+1];
      for(int i=0; i<inputArray.length; i++){
        newInputArray[i]=inputArray[i];
      }
      newInputArray[inputArray.length]=input
      intputArray=newInputArray;
    }while(!input.equals("done"));
    

    未经测试的代码,对它持保留态度。

    【讨论】:

    • 当然,拒绝那些试图帮助你的人。
    • 哎呀,对不起,我也是新人:)
    【解决方案4】:
    ArrayList<String> names = new ArrayList<String>();
    String userInput;
    Scanner scanner = new Scanner(System.in);
    while (true) {
        userInput = scanner.next();
        if (userInput.equals("done")) {
            break;
        } else {
            names.add(userInput);
        }
    }       
    scanner.close();
    

    【讨论】:

    • 因为使用中断而不是循环条件更难阅读。 while (!userInput.equals("done")) 在阅读代码时更有意义
    • 代码的可读性稍差,现在足以让我们在这里投反对票了吗?
    • 我没有投反对票,只是说明为什么这个答案可能不如另一个
    猜你喜欢
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2020-10-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多