gpt4 book ai didi

java - 重复附加到字符串时无法添加换行符

转载 作者:行者123 更新时间:2023-11-29 05:02:09 24 4
gpt4 key购买 nike

我正在尝试写入具有特定格式的文本文档。这是我现在拥有的。

    String line = "";
double totalCost = 0;

Node curr = summary.head.next;
while(curr!=summary.tail)
{
line += [an assortment of strings and variables] +"\r";
totalCost += PRICELIST.get(curr.itemName)*curr.count;
curr = curr.next;
}

write.printf("%s" + "%n", line);

这就是添加到线上的部分实际的样子。

"Item's name: " + curr.itemName + ", Cost per item: " +  NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName)) +
", Quantity: " + curr.count + ", Cost: " + NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName)*curr.count) + "\r";

我也尝试过使用换行符。在我让它工作之前,当打印语句在循环内意味着它一次只写一行。我想这样做是因为我将有多个线程写入这个文件,这样任何线程都不会持有锁太久。

最佳答案

如果使用 Java 7 或更高版本,您可以使用 System.lineSeparator()

关于java - 重复附加到字符串时无法添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com