下面的代码显示了如何从console接受多行输入。但在这种情况下,我无法为处理结果的输入提供EOF。所以程序没有编译。请提出一些解决问题的方法。
package Controller;
import java.util.*;
public class Multi_Line_Input
{
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please enter the inputs");
List<String> l = new ArrayList<String>();
while(s.hasNext())
{
String line = s.nextLine();
l.add(line);
}
Iterator i = l.iterator();
while(i.hasNext())
{
System.out.println((String)i.next());
}
}
}
答案 0 :(得分:1)
这是你在找什么?当您键入退出时,它将停止程序并打印您输入的行。
import java.util.*;
public class Multi_Line_Input
{
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please enter the inputs");
List<String> l = new ArrayList<String>();
while(s.hasNext())
{
String line = s.nextLine();
if (line.equalsIgnoreCase("exit")) {
break;
}
l.add(line);
}
Iterator i = l.iterator();
while(i.hasNext())
{
System.out.println((String)i.next());
}
}
}