【问题标题】:How to scan a user's new input using a scanner in a while loop?如何在while循环中使用扫描仪扫描用户的新输入?
【发布时间】:2021-10-01 04:21:51
【问题描述】:

我开发了一个字符排序器,我想在每次字符串排序后提示用户输入一个新字符串。我遇到的问题是扫描仪不断扫描用户的第一个输入。如果我使用scanner.next(),它不会计算输入末尾的空白,这不是解决方案。

这是 while 循环的开始。代码完成后,它会再次从“inputText”开始。

    inputText:
    while(keepSorting){

        System.out.print("Please input the string to be sorted: " );
        c.settingInput(s.nextLine());
        System.out.println(" ");
        int option;

        more code
        .... }

【问题讨论】:

  • nextLine 会给你下一行。不知道你的意思。
  • 你想留在while循环中吗?如果是这样,一定有一些代码行重定向到inputText:
  • 如果你需要帮助调试你的代码,那么你需要发布一个minimal reproducible example

标签: java


【解决方案1】:
Scanner sc= new Scanner(System.in); 
while(true){
String a = sc.nextLine();
System.out.println(a);

}

如果您想了解有关 Scanner 的更多信息,请阅读 this 文档。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以在help center找到更多关于如何写好答案的信息。
  • @LizeshShakya 已编辑
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多