0
我正在實現類內的Formattable接口的formatTo()方法。然後我現在用它的printf()參數,如下所示:Java可格式化接口和printf參數
public static void main(String[] args)
{
BankAccount1 bankAccount = new BankAccount1(1234.12) ;
//Don't understand this code
System.out.printf("%s %10S.\n", "Balance = ", bankAccount) ;
}
class BankAccount implements Formattable
{
public void formatTo(Formatter formatter, int flags, int width, int precision)
{
Appendable appendable = formatter.out() ;
String balanceString = "" + balance ;
for (int i = balanceString.length() ; i < width ; i++)
balanceString = "$" + balanceString ;
try {
//tells appendable to append balanceString to printf's string
appendable.append(balanceString) ;
}
catch (IOException e) {
System.exit(0) ;
}
}
}
,輸出是:
Balance = $$$1234.12.
我的問題是,上線:
System.out.printf("%s %10S.\n", "Balance = ", bankAccount) ;
我知道%s用於字符串,但是什麼是「%10S。\ n」呢?此外,在「%10S。\ n」爲什麼「%10S」爲什麼資本S,它代表什麼?
你寫了這段代碼吧?爲什麼你**使用大寫字母'S'而不是小寫char?你也不應該在'printf'中使用'/ n',而應該使用與平臺無關的'%n'。 –
不,不是我的代碼。 –