2014-07-18 20 views
8

Çok basit bir soruyu biliyorum. ancak boolean operatörü için stringformat'ı bilmek istiyorum. Örneğin, tamsayı, dize ve kayan için dize biçimlerini gösterir. boolean operatörü için ne doğru/yanlış olabilir?Java Boole İşlevi için StringFormat

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); 
+1

http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html [printf kullanarak biçimlendirme ait –

+1

olası yinelenen ve format] (http://stackoverflow.com/questions/8629995/formatting-using-printf-and-format) – Jens

cevap

23

'b' veya 'B' genel Eğer bakıyoruz bu

float floatVar=1.0f; 
    int intVar=1; 
    String stringVar="hi"; 
    boolean boolVar=false; 
    System.out.printf("The value of the float " + 
        "variable is %f, while " + 
        "the value of the " + 
        "boolean variable is %b, " + 
        "and the string is %s", 
      floatVar, boolVar, stringVar); 

%b edilir deneyebilirsiniz argüman arg null, sonra sonuç "false" dir. Arg bir boolean veya Boole ise, sonuç String.valueOf (arg) tarafından döndürülen dizedir. Aksi halde sonuç "doğru" dır. java dokümanlar: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

enter image description here

1

boole'ye yönelik tutucu %b

7
System.out.printf("boolean variable is %b",boolVar); 
1

olduğunu Sen

1

System.out bir PrintStream ve tüm dönüşümlerin bir tablo vardır format stream syntax için PrintStream.printf bağlantılar için belgelerdir. Bu tablodaki ilk giriş:

'b', 'B' - arg argümanı null ise, sonuç "false" olduğunu. arg bir boolean veya Boolean ise, sonuç String.valueOf(arg) tarafından döndürülen dizedir. Aksi halde, sonuç "true".

0

Bir daha yoludur -

String output = String.format("boolean variable is %b",true); 
    System.out.print(output);