[英]Formatting output in java assistance
想知道是否有人可以進一步指導我使用寬度說明符使我的輸出看起來像這樣只使用靜態字符串格式方法。
ID:1122
創建時間:03/05/14
所有者:Sam Williams
期初余額:14600.0美元
年費率:4.5
提款:5400.0
當前余額:14600.0美元
目前,我的輸出是這樣的(使用標簽和新行)
ID:1122
創建時間:03/05/14
所有者:Sam Williams
期初余額:14600.0美元
年費率:4.5
提款:5400.0
當前余額:14600.0美元
JAVA代碼目前是:
System.out.println("ID:\t\t " + a.getId());
System.out.println("Created:\t" + String.format("%tD", a.getDateCreated()));
System.out.println("Owner:\t\t" + "" + a.getOwner() +
"\nOpening Balance: $" + a.getBalance() + "\nAnnual Rate:\t" + a.getAnnualRate() +"\nWithdraw:\t" + withDrawal +"\nCurrent Balance:\t" + " $ " + String.format("%.2f", a.getBalance()));
您可以在java中使用format
方法。 它看起來像:
System.out.format("The Name is of %d is %f.%n", i, r);
編輯:一般語法是: %[flags][width][.precision][argsize]typechar
它適用於所有數據類型。 它類似於C中的格式說明符。
您最好學習Java中字符串格式的基本用法,如十進制,浮點數,字符串,日期和時間。
例如,您可以使用第一行執行此操作:
System.out.println(String.format("ID:%12d" , a.getId()));
這里12是id的最大長度,如果你的長度(id)<12,那么左邊會插入一些空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.