2014-07-18 16 views
8

So che è una domanda molto semplice. ma mi piacerebbe conoscere il formato stringa per l'operatore booleano. Ad esempio, di seguito sono riportati i formati stringa per intero, stringa e float. cosa potrebbe essere per l'operatore booleano vero/falso?StringFormat per Java Boolean Operator

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

possibile duplicato del [formattazione utilizzando printf e formato] (http://stackoverflow.com/questions/8629995/formatting-using-printf-and-format) – Jens

risposta

1

Il segnaposto per booleana è %b

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

si può provare questo

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 è che si sta guardando

1

System.out è un PrintStream e la documentazione per PrintStream.printf collegamenti alla format stream syntax che ha un tavolo di tutte le conversioni. La prima voce nella tabella stessa:

'b', 'B' - Se l'argomento arg è null, allora il risultato è "false". Se arg è un boolean o Boolean, il risultato è la stringa restituita da String.valueOf(arg). In caso contrario, il risultato è "true".

0

Un altro modo è quello -

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