首先,这是我采用的第一个编程类,所以我很抱歉如果这是一个非常简单的问题。对于作业分配,我们必须编写扫描文本文件的程序,提示用户输入< >,然后将结果写入另一个文本文件。

我遇到的问题是输出文件全部写在一行上,而不是保留输入文件中的换行符。

例如:

输入

好像

本文。

输出看起来像这样的文字。

这是我在相关方法中的代码。

public static void createMadLib(Scanner console) throws FileNotFoundException {
Scanner input = getInput(console);
System.out.print("Output file name: ");
PrintStream out = new PrintStream(new File(console.nextLine()));
System.out.println();
String text = input.next();
while (input.hasNext()){
if (text.startsWith("
text = text.substring(1, text.length()-1);
text = text.replace("-"," ");
if ((text.startsWith("a"))||(text.startsWith("e"))||(text.startsWith("i"))||(text.startsWith("o"))||(text.startsWith("u"))){
System.out.print ("Please type an " + text + ": ");
text = (console.nextLine());
}else {
System.out.print ("Please type a " + text + ": ");
text = (console.nextLine());
}
}
out.print(text + " ");
text = input.next();
}//ends while loop
out.print(text);
prompt(console);
}

我为任何格式错误的失礼道歉,再次,这是我的第一个编程类。

谢谢你的帮助