【问题标题】:StringFormat for Java Boolean OperatorJava 布尔运算符的 StringFormat
【发布时间】:2014-09-09 05:40:52
【问题描述】:

我知道它非常简单的问题。但我想知道布尔运算符的字符串格式。 例如,下面显示了整数、字符串和浮点数的字符串格式。布尔运算符 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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    System.out.printf("boolean variable is %b",boolVar);
    

    【讨论】:

      【解决方案3】:

      布尔值的占位符是%b

      【讨论】:

        【解决方案4】:

        另一种方法是-

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

        【讨论】:

          【解决方案5】:

          System.out 是一个PrintStreamPrintStream.printf 的文档链接到format stream syntax,其中包含所有转换的表格。该表中的第一项:

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

          【讨论】:

            【解决方案6】:

            你可以试试这个

                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你在看吗

            【讨论】:

              猜你喜欢
              • 2013-03-02
              • 1970-01-01
              • 2011-04-20
              • 1970-01-01
              • 2011-09-27
              • 2011-03-27
              相关资源
              最近更新 更多