这是代码:
public class Pr3
{
public Pr3 ()
{
String x = "";
String Line = "";
Scanner sc = new Scanner (System.in);
do
{
System.out.println ("Please enter your sentence");
String NewLine = sc.nextLine();
Line = Line + " " + NewLine;
System.out.println ("Your new Line is: " + Line);
System.out.println ("Do you want to enter a new sentence? Enter Y for yes and N for no");
x = sc.next();
} while (!"N".equals(x) && "Y".equals(x));
exit(0);
}
}这是输出:
发布于 2015-12-19 11:01:02
变化
x = sc.next();至
x = sc.nextLine();以使用行字符的末尾。
除此之外,
Line = Line + " " + NewLine;将新行连接到前面的所有行。我不确定这是你想要的(根据你打印的System.out.println ("Your new Line is: " + Line);)。
最后一件事:
while (!"N".equals(x) && "Y".equals(x));是多余的。如果x等于"Y",那么它就不等于"N",因此就足以编写:
while ("Y".equals(x));https://stackoverflow.com/questions/34369871
复制相似问题