Java 布尔运算符的 StringFormat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24818989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:00:46  来源:igfitidea点击:

StringFormat for Java Boolean Operator

java

提问by Umesh Patil

I know its very simple question. but I would like to know the stringformat for boolean operator. For example, below shows the string formats for integer, string and float. what could be for boolean operator 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); 

采纳答案by LMK

'b' or 'B' general If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true". java docs : http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

'b' 或 'B' 一般 如果参数 arg 为空,则结果为“false”。如果 arg 是布尔值或布尔值,则结果是 String.valueOf(arg) 返回的字符串。否则,结果为“真”。java 文档:http: //docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

enter image description here

在此处输入图片说明

回答by Jens

The placeholder for boolean is %b

布尔值的占位符是 %b

回答by Zhenxiao Hao

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

回答by Ruchira Gayan Ranaweera

You can try this

你可以试试这个

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

%bis you are looking at

%b你在看吗

回答by Chris Martin

System.outis a PrintStreamand the documentation for PrintStream.printflinks to the format stream syntaxwhich has a table of all of the conversions. The first entry in that table:

System.out的的PrintStream和文档PrintStream.printf链接到格式流语法具有所有的转换表。该表中的第一个条目:

'b', 'B'- If the argument argis null, then the result is "false". If argis a booleanor Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".

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

回答by Ninad Pingale

One more way is -

另一种方法是——

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