在我的方法中,我只是想把它打印出来:
此致
鲍勃
public String getText() {
letter = ("Dear " + toPerson + ":\n\n"); // Dear recipient name & blank line
body = body.concat("\nSincerely,").concat("\n"); // sincerely & blank line
letter = letter.concat(body + fromPerson); // adds body
System.out.println(letter);
return letter;
}
然而,打印出来的是:
真诚地没有空间
如果我添加另一个\n
而不是出于某种原因,它会重复两次!
我的信件类:
public Letter(String from, String to) {
fromPerson = from;
toPerson = to;
}
public void addLine(String line) {
body = body.concat(line).concat("\n"); // body lines
}
public String getText() {
letter = ("Dear " + toPerson + ":\n\n"); // Dear recipient name & blank line
body = body.concat("\nSincerely,").concat("\n"); // sincerely & blank line
letter = letter.concat(body + fromPerson); // adds body
System.out.println(letter);
return letter;
}
}
这就是它被称为
的地方public class Program {
public static void main(String[] args) {
Letter myLetter = new Letter("Another Person", "Person");
myLetter.addLine("I am sorry we must part.");
myLetter.addLine("I wish you all the best.");
myLetter.getText();
}
}
答案 0 :(得分:-1)
然而,打印出来的是:
真诚地没有空间
只需添加其他\n
body = body.concat("\nSincerely,\n\n");
或汤姆的回答
body = body.concat("\nSincerely,").concat("\n\n");