我們知道輸出格式化數(shù)字可以使用printf()和format()方法。String類使用靜態(tài)方法format()返回一個(gè)String對象而不是PrintStream對象。
String類的靜態(tài)方法format()能用來創(chuàng)建可復(fù)用的格式化字符串,而不僅僅是用于一次打印輸出。如下所示:
System.out.printf("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatVar, intVar, stringVar);
你也可以這樣寫
String fs; fs = String.format("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatVar, intVar, stringVar); System.out.println(fs);
![]() | ![]() .. 定價(jià):¥45 優(yōu)惠價(jià):¥42 更多書籍 |
![]() | ![]() .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書籍 |