簡體   English   中英

是否可以在Java println語句的中心插入計數?

[英]Is it possible to insert a count in the center of a Java println statement?

我是java的新手,正在使用小費計算器,如果有多個客戶拆分支票,我需要一個計數器來確定小費結果。

輸出問題表示為

“客戶1的帳單金額(以美元為單位)是多少?”

如果用戶要輸入有多個客戶,我將如何更改該語句以使輸出為

“客戶2的帳單金額(以美元為單位)是多少?

等等等等。

我試圖在我的語句的中心輸入計數器以將數字增加一,但是我一直收到語法錯誤。

int count = i;
System.out.println("What was customer "+count+"'s bill amount (in dollars)?");

您需要連接輸出:假設您的counter是一個稱為count的整數

System.out.println("What was customer " + count + "'s bill amount (in dollars)?");

您還可以使用printf ,它可以編寫類似於格式化String

System.out.printf("What was customer %d's bill amount (in dollars) ?", count);

要添加更多內容,您還可以使用StringBuilder制作String:

StringBuilder builder  = new StringBuilder(String.format("What was customer %d's bill amount (in dollars)?", count));

    System.out.println(builder);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM