2014-07-18 10 views
8

Znam bardzo proste pytanie. ale chciałbym znać format string dla operatora boolean. Na przykład poniżej pokazano formaty ciągów dla liczb całkowitych, łańcuchów i liczb zmiennoprzecinkowych. co może być dla operatora boolowskiego prawda/fałsz?StringFormat dla operatora Java Boolean

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 –

+1

możliwe duplikat [Formatowanie przy użyciu printf i formatowanie) (http://stackoverflow.com/questions/8629995/formatting-using-printf-and-format) – Jens

Odpowiedz

1

zastępczy dla wartości logicznej jest %b

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

Można spróbować tej

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 jest szukasz w

1

System.out jest PrintStream i dokumentacja dla PrintStream.printf linki do format stream syntax która ma tabelę wszystkich konwersji. Pierwszy wpis w tej tabeli:

'b', 'B' - Jeśli argument arg jest null, wtedy wynik jest "false". Jeśli arg jest boolean lub Boolean, wynikiem jest ciąg znaków zwrócony przez String.valueOf(arg). W przeciwnym razie wynikiem jest "true".

0

Jednym ze sposobów jest więcej -

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