刚学java,在保留小数点的过程中,使用String.format()函数始终报错
package decimalsAdd;
import java.text.DecimalFormat;
import javax.sound.sampled.AudioSystem;
public class decimalsAdd {
public static void main(String[] args) {
int n = 30;
double sum = 0.0;
for(int i = 1, j = 1; i <= n; i++,j *= 3) {
sum += (double)i/(double)j;
System.out.println(i + " " +sum);
}
//System.out.println("前" + n + "项之和为:" + sum);
System.out.println("前" + n + "项之和为:" + String.format("asd %d", sum));
//这里进行了其他尝试
System.out.println(String.format("字符串:%s", "String"));
System.out.println(String.format("字符:%c", 'M'));
System.out.println(String.format("布尔类型:%b", 'M'>'A'));
System.out.println(String.format("八进制整数类型:%o", 17));
System.out.println(String.format("十进制整数类型:%d", 17));
System.out.println(String.format("十六进制整数类型:%x", 17));
System.out.println(String.format("基本浮点类型:%f", 99.1));
System.out.println(String.format("指数类型:%e", 100.111111111));
System.out.println(String.format("十六进制浮点类型:%a", 17.111111));
System.out.println(String.format("散列码:%h", 17));
System.out.println(String.format("百分比类型:17%%"));
System.out.print(String.format("换行符:%n", 17));
}
}
/*所有的报错信息都是:
The method format(String, Object[]) in the type String is not applicable for the arguments (String, char)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, double)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, String)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, boolean)这些类似
*/
这里是截图:
我检查String.class中format()
并没有不对,想知道问题出在哪?jdk版本是jdk-21,eclipse是23年9月的版本。