Java 我必须让用户输入一个循环,直到输入“完成”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20589178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:58:07  来源:igfitidea点击:

I have to make a loop taking a users input until "done" is entered

javastringloopsarraylist

提问by yoyo

I'm trying to make an ArrayListthat takes in multiple names that the user enters, until the word doneis inserted but I'm not really sure how. How to achieve that?

我正在尝试ArrayList使用用户输入的多个名称,直到done插入单词,但我不确定如何。如何做到这一点?

采纳答案by zbr

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();

回答by Kent

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"));

untested code, take it with a grain of salt.

未经测试的代码,请谨慎对待。

回答by Elliott Frisch

I would probably do it like this -

我可能会这样做 -

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.
  }
}

回答by Sina

    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);