String.format()不能正常使用

public static void main(String[] args) {
String name = "David";
int age = 18;
String hobby = "篮球";
String formatString = "我的名字是%s,我今年%d岁,我的爱好是%s";
String output = String.format(formatString,name,age,hobby);
System.out.println(output);
报错:The method format(String, Object[]) in the type String is not applicable for the arguments (String, String, int, String)

2个回答

没问题啊,编译环境 Java8

图片说明

Gonana_Nekone
Gonana_Nekone jdk环境配置有问题吗
14 天之前 回复

```package com;

public class Test {

public static void main(String[] args) {
    String name = "David";
    int age = 18;
    String hobby = "篮球";
    String formatString = "我的名字是%s,我今年%d岁,我的爱好是%s";
    String output = String.format(formatString, name, age, hobby);
    System.out.println(output);
}

}

E:\jdk\jdk1.8\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53184,suspend=y,server=n -javaagent:C:\Users\10240\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "E:\jdk\jdk1.8\jre\lib\charsets.jar;E:\jdk\jdk1.8\jre\lib\deploy.jar;E:\jdk\jdk1.8\jre\lib\ext\access-bridge-64.jar;E:\jdk\jdk1.8\jre\lib\ext\cldrdata.jar;E:\jdk\jdk1.8\jre\lib\ext\dnsns.jar;E:\jdk\jdk1.8\jre\lib\ext\jaccess.jar;E:\jdk\jdk1.8\jre\lib\ext\jfxrt.jar;E:\jdk\jdk1.8\jre\lib\ext\localedata.jar;E:\jdk\jdk1.8\jre\lib\ext\nashorn.jar;E:\jdk\jdk1.8\jre\lib\ext\sunec.jar;E:\jdk\jdk1.8\jre\lib\ext\sunjce_provider.jar;E:\jdk\jdk1.8\jre\lib\ext\sunmscapi.jar;E:\jdk\jdk1.8\jre\lib\ext\sunpkcs11.jar;E:\jdk\jdk1.8\jre\lib\ext\zipfs.jar;E:\jdk\jdk1.8\jre\lib\javaws.jar;E:\jdk\jdk1.8\jre\lib\jce.jar;E:\jdk\jdk1.8\jre\lib\jfr.jar;E:\jdk\jdk1.8\jre\lib\jfxswt.jar;E:\jdk\jdk1.8\jre\lib\jsse.jar;E:\jdk\jdk1.8\jre\lib\management-agent.jar;E:\jdk\jdk1.8\jre\lib\plugin.jar;E:\jdk\jdk1.8\jre\lib\resources.jar;E:\jdk\jdk1.8\jre\lib\rt.jar;F:\java_workSpace\Test\out\production\Test;D:\tools\IntelliJ IDEA 2020.1\lib\idea_rt.jar" com.Test
Connected to the target VM, address: '127.0.0.1:53184', transport: 'socket'
我的名字是David,我今年18岁,我的爱好是篮球
Disconnected from the target VM, address: '127.0.0.1:53184', transport: 'socket'

没问题啊,是不是环境变量/jdk配置的有问题啊
Gonana_Nekone
Gonana_Nekone 回复math332: 下了个IDEA2020真的可以了,谢谢!
14 天之前 回复
math332
math332 回复Gonana_Nekone: 可能是开发工具和JDK版本不匹配,你用IDEA2020 jdk1.8试试,肯定没问题
14 天之前 回复
Gonana_Nekone
Gonana_Nekone 我按照网上的环境配置教程重新看了下似乎没问题
14 天之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问