英文:
Java - using /r within one System.out
问题
以下是翻译好的内容:
我试图在一个 System.out 语句中获取几行分开的文本。应该有4行分开的文本,但程序只打印了最后一行。
我考虑将其替换为每行使用单独的 System.out,但我不确定是否是最快的解决方案,以及当前方案有什么问题。
包含上述代码的方法:
public double getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4){
this.additionPrices = ("第一个附加项 - " + addition1.getName() + " 价格为 " + addition1.getPrice() + "\r" +
"第二个附加项 - " + addition2.getName() + " 价格为 " + addition2.getPrice() + "\r" +
"第三个附加项 - " + addition3.getName() + " 价格为 " + addition3.getPrice() + "\r" +
"第四个附加项 - " + addition4.getName() + " 价格为 " + 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();
}
结果:
基础价格为:20.0
总价格为:26.5
第四个附加项 - 洋葱 价格为 1.5
进程已退出,退出码为 0
如果这有什么显而易见的地方,我很抱歉。这是我在这里的第一个问题,我在旧问题中找不到答案。
英文:
I was trying to get a few separated lines of text within one System.out. There was supposed to be 4 separate lines and instead of that program only prints the last one.
I was thinking about replacing it with separate System.out for each of the lines but i'm not sure if it's the fastest solution and what is wrong with the current one.
Method that contains above mentioned code:
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());
}
Main class where it's used:
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();
}
}
Result:
Base price equals: 20.0
Total price equals: 26.5
Fourth addition - Onion costs 1.5
Process finished with exit code 0
Sorry if that's something obvious. It's my first question here and I couldnt find the answer in old questions.
答案1
得分: 1
Use line feed \n
instead of \r
.
In terminals that support it, the carriage return character \r
returns the cursor to the beginning of the line. It gives you a way to delete and overwrite the current line.
In comparison, the line feed character \n
moves the cursor to the next line.
英文:
Use line feed \n
instead of \r
.
In terminals that support it, the carriage return character \r
returns the cursor to the beginning of the line. It gives you a way to delete and overwrite the current line.
In comparison, the line feed character \n
moves the cursor to the next line.
答案2
得分: 0
> 我试图在一个 System.out 中获取几行分隔的文本。
在 Unix 系统中,行使用 \n
字符进行分隔,而不是会将光标重置到行首的 \r
字符。在你的情况下,每一行都在覆盖前一行。
但实际上,你根本不需要使用 \n
。你可以这样做:
System.out.println("第一个附加品 - " + addition1.getName() + " 价格为 "
+ addition1.getPrice());
System.out.println("第二个附加品 - " + addition2.getName() + " 价格为 "
+ addition2.getPrice() + "\r" );
println(...)
会打印消息,然后添加“行分隔符”字符,这是可移植的,因此它可以在其他不使用 \n
作为行尾的操作系统下工作。这意味着你不需要直接使用 \n
字符。
> 我正在考虑将其替换为每行单独的 System.out,但我不确定这是否是最快的解决方案,以及当前方案有什么问题。
这是典型的过早优化。不用担心多次调用 System.out
,因为它们无论如何都会进行 IO 操作,所以使用的额外资源几乎可以忽略不计(如果有的话,也是纳秒级别的)。在优化后的代码中出现错误,正是为什么我们应该编写可维护和可读的代码的完美例子,只有在必要时才进行优化。
英文:
> I was trying to get a few separated lines of text within one System.out.
Lines in unix are separated by the \n
character instead of the \r
character which resets the cursor to the front of the line. In your case, each of your lines was overwriting the previous one.
But you really should not have to do the \n
at all. You should do something like:
System.out.println("First addition - " + addition1.getName() + " costs "
+ addition1.getPrice());
System.out.println("Second addition - " + addition2.getName() + " costs "
+ addition2.getPrice() + "\r" );
println(...)
prints the message and then adds the "line-separator" character which is portable so it works under other operating systems which don't use \n
for their end-of-line. This means that you don't have to use the \n
character directly.
> I was thinking about replacing it with separate System.out for each of the lines but i'm not sure if it's the fastest solution and what is wrong with the current one.
This is textbook premature optimization. Don't worry about the multiple System.out
calls since they are doing IO anyway so the minuscule extra resources that may be used (you are talking about nanoseconds if any) aren't going to be noticed. That you had a bug in the optimized code is a perfect example of why we should work for maintainable and readable code and only optimize when necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论