Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有人能看看我的密码吗;在整数中为%s指定其他选项吗?_Java - Fatal编程技术网

Java 有人能看看我的密码吗;在整数中为%s指定其他选项吗?

Java 有人能看看我的密码吗;在整数中为%s指定其他选项吗?,java,Java,%s是如何为int工作的 package loops; public class Checking { public static void main(String[] args) { String formatter = "hello"; int formatter1 = 2; System.out.printf("%s friend %n",formatter); System.out.printf("%

%s是如何为int工作的

   package loops;

    public class Checking {
    public static void main(String[] args) {
        String formatter = "hello";
        int formatter1 = 2;
        System.out.printf("%s friend %n",formatter);
        System.out.printf("%s cup of coffee please",formatter1);
        /* problem is
        %s stands for string
        How %s is work for integer in formatter 1 (System.out.printf("%s cup of coffee please",formatter1);
        "i am beginner in java,
        */
    }
    }
输出是正确的


但我想知道整数还有其他选择吗?

整数的符号是
%d
(请参阅)。而
%s
只需调用
Integer.toString
就可以处理int

从文档:

's','s'如果
arg
实现
Formattable
,则调用
arg.formatTo
。否则,通过调用
arg.toString()

因此,当您传递一个
int
is时,它会自动装箱到
Integer
,并在该
Integer
上调用方法
.toString
,因此您的
int
将成为
字符串


一般来说,我建议您阅读本手册,尤其是如果您是一名程序员新手。作者的许多智慧被提炼到文档中。

整数的符号是
%d
(请参阅)。而
%s
只需调用
Integer.toString
即可用于
int
。请参阅:为什么要使用
%s
作为开头?文档对于理解正在发生的事情可能很重要:@CarlosHeuberger我喜欢使用它……是否会导致任何错误问题??[我是java初学者]我不认为这是个问题,但“喜欢使用”是一种奇怪的编程技术。。。问题的原因可能是不知道它的作用。