应该将哪种printf转换用于布尔值?

问题描述 投票:0回答:6

我知道这是一个非常简单的问题,但我想知道布尔类型的字符串格式。例如,下面显示了用于整数,字符串和浮点的字符串格式。布尔值
true
/
false

的适当格式是什么?
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);
java formatting printf
6个回答

51
投票
System.out.printf("boolean variable is %b",boolVar);

14
投票

更多方法是-

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

10
投票

布尔的占位符为

%b


5
投票

System.out是一个PrintStream,并且用于PrintStream.printf的文档链接到Format流语法,其中包含所有转换的表。该表中的第一个条目:

'b'

'B'
-如果参数
argnull
,则结果为
"false"
。如果
argboolean
Boolean
,则结果是由
String.valueOf(arg)
返回的字符串。否则,结果是
"true"


3
投票

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

你在看

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.