【发布时间】:2020-11-13 16:33:38
【问题描述】:
我试图在一个 System.out 中获得几行单独的文本。应该有 4 行单独的行,而不是该程序只打印最后一行。
我正在考虑用单独的 System.out 为每一行替换它,但我不确定它是否是最快的解决方案以及当前的解决方案有什么问题。
包含上述代码的方法:
public double getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4){
this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\r" +
"Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\r" +
"Third addition - " + addition3.getName() + " costs " + addition3.getPrice() + "\r" +
"Fourth addition - " + addition4.getName() + " costs " + addition4.getPrice());
this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice();
return (this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice());
}
使用它的主类:
public static void main(String[] args) {
Meat meat = new Meat();
Roll roll = new Roll();
Addition tomato = new Tomatos(2);
Addition lettuce = new Lettuce(1);
Addition pickle = new Pickle(3);
Addition onion = new Onion(1.5);
Hamburger hamburger = new Hamburger(meat,roll,20);
hamburger.getTotal(onion,onion, tomato, onion);
hamburger.showPrice();
}
}
结果:
Base price equals: 20.0
Total price equals: 26.5
Fourth addition - Onion costs 1.5
Process finished with exit code 0
对不起,如果这很明显。这是我在这里的第一个问题,我无法在旧问题中找到答案。
【问题讨论】:
-
这能回答你的问题吗? What is the difference between a "line feed" and a "carriage return"?。
\r完全按预期工作,并不断覆盖同一行。如果你想要单独的行,你必须使用\n -
确实如此,谢谢。我在搜索时使用了错误的关键字。
标签: java