如何使用"\n"来格式化toString()方法?

huangapple go评论87阅读模式
英文:

How to format toString() method with "\n"?

问题

以下是您的翻译内容:

我有一个toString()方法,它返回关于在线订购的信息。此方法返回个人的姓名,购物品牌,购买物品的价格,总价格以及物品是否可用。

根据订单人数,我希望能够多次输出以下内容:

Amazon:订单编号1001
客户:“姓名”
品牌:“卖家”
价格:$“双精度中的价格”
“物品名称”
总价格:$“双精度中的价格”

我希望,如果人们没有订购任何物品,将会隐藏“物品名称”部分,并且不包括"\n"

输出示例:

Amazon:订单编号1002
客户:简·多e
品牌:耐克
价格:$0.0
总价格:$0.0

Amazon:订单编号1001
客户:简·多e
品牌:耐克
价格:$94.04
耐克Air Max 97
总价格:$94.04

Amazon:订单编号1001
客户:简·多e
品牌:耐克
价格:$86.90
耐克Revolution 5
总价格:$180.94

除了在人们未订购物品时会创建新行外,我的输出执行方式如下所示。您可以在下面看到,第一个订单在价格和总价格之间有一个空格。

我的输出:

Amazon:订单编号1002
客户:简·多e
品牌:耐克
价格:$0.0
总价格:$0.0

Amazon:订单编号1001
客户:简·多e
品牌:耐克
价格:$94.04
耐克Air Max 97
总价格:$94.04

Amazon:订单编号1001
客户:简·多e
品牌:耐克
价格:$86.90
耐克Revolution 5
总价格:$180.94

这是我的toString()方法:

public String toString() {
    String item = "";
    for (int i = 0; i < size; i++) {
        item += items[i];
        if (i < currentNumOrders - 1) {
            item += "\n";
        }
    }

    return app + ": 订单编号#" + number + "\n客户:" + name + "\n品牌:" + brand +
           "\n价格:$" + price + "\n" + item + "总价格:$" + sum;
}
英文:

I have a toString() method where it returns information about ordering online. This returns the person's name, brand the person is shopping at, price of what they're buying, total price and if it's available or not.

My criteria is to have multiple output of the following based on how many people orders:

Amazon: Order #1001
Customer: &quot;Name&quot;
Brand: &quot;Seller&quot;
Price: $&quot;Price in double&quot;
&quot;Name of item&quot;
Total Price: $&quot;Price in double&quot;

I want so that if the person orders nothing, the "name of item" portion disappears and doesn't include the &quot;\n&quot;.

Output Example:

Amazon: Order #1002
Customer: Jane Doe
\nBrand: Nike
Price: $0.0
Total Price: $0.0

Amazon: Order #1001
Customer: Jane Doe
Brand: Nike
Price: $94.04
Nike Air Max 97
Total Price: $94.04

Amazon: Order #1001
Customer: Jane Doe
Brand: Nike
Price: $86.90
Nike Revolution 5
Total Price: $180.94

My output does everything as follows except it creates a new line when the person doesn't order anything. You can see this below where the first order has a space between price and total price.

My output:

Amazon: Order #1002
Customer: Jane Doe
Brand: Nike
Price: $0.0

Total Price: $0.0

Amazon: Order #1001
Customer: Jane Doe
Brand: Nike
Price: $94.04
Nike Air Max 97
Total Price: $94.04

Amazon: Order #1001
Customer: Jane Doe
Brand: Nike
Price: $86.90
Nike Revolution 5
Total Price: $180.94

This is my toString() method:

public String toString() {
    String item = &quot;&quot;;
    for(int i = 0; i &lt; size; i++) {
	    s += s[i];
	    if(i &lt; currentNumOrders - 1) {
		    item += &quot;\n&quot;;
	    }
    }  

    return app + &quot;: Order #&quot; + number + &quot;\nCustomer: &quot; + 
           name + &quot;\nBrand: &quot; + brand + &quot;\nPrice: $&quot; + 
           price + &quot;\n&quot; + item + &quot;Total Price: $&quot; + sum;       
}

答案1

得分: 2

你可以使用三元条件来在必要时显示 \n

return app + ":订单 #" + number + "\n客户:" + 
           name + "\n品牌:" + brand + "\n价格:$" + 
           price + "\n" + (item.length() > 0 ? item + "\n" : "") + "总价格:$" + sum;  
英文:

You can use ternary condition to show \n only when neccessary:

return app + &quot;: Order #&quot; + number + &quot;\nCustomer: &quot; + 
           name + &quot;\nBrand: &quot; + brand + &quot;\nPrice: $&quot; + 
           price + &quot;\n&quot; + (item.length() &gt; 0 ? item + &quot;\n&quot; : &quot;&quot;) + &quot;Total Price: $&quot; + sum;  

huangapple
  • 本文由 发表于 2020年10月8日 06:54:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定