0

我想要的是能够在控制台中输入一个字符串并将其输入到一个字符串中(一个数组很好,我只想存储它)。这是我的示例代码:

System.out.println("What is your opinion?");
        while(user_input.hasNext()){
            choice = choice + " " + user_input.next();
        }
        this.opinion = choice;

所有这一切都是重复要求用户输入。提前感谢您的任何意见!

4

2 回答 2

2

看起来你想要nextLine()

System.out.println("What is your opinion?");
this.opinion = user_input.nextLine();

请注意,您还应该遵循 Java 命名约定:userInput.

于 2013-10-09T03:07:27.900 回答
1
System.out.println("What is your opinion?");

  Scanner user_input = new Scanner(System.in);
  String line = user_input.nextLine();
  String choice = "";
  while (!line.equalsIgnoreCase("-1")){ // here you can have terminate string. I used -1
     choice += "," + line; 
     line = user_input.nextLine();
  }
  System.out.println(choice);

然后你可以得到一个字符串数组

String[] words = choice.split(",");
于 2013-10-09T03:21:31.167 回答