gpt4 book ai didi

java - 我需要使这段代码正常工作,但无法找出问题所在。这是一个 System.out.format() 问题

转载 作者:行者123 更新时间:2023-12-02 05:55:33 24 4
gpt4 key购买 nike

我似乎无法格式化此代码。我不断收到此错误:

Exception in thread 
"main" java.lang.Error: Unresolved compilation problem:
The method format(String, Object[]) in the type PrintStream is not applicable
for the arguments (String, int, double) at
org.lineware.learningjava.Root2.main(Root2.java:9)

代码如下:

public class Root2 {

public static void main(String[] args) {
int i = 2;
double r = Math.sqrt(i);
System.out.format("The square root of %d is %f.%n", i, r);
}
}

最佳答案

Java 5 中添加了变量参数 (varargs)。在此之前,您尝试使用的 format(..) 方法被声明为

public PrintStream format(String format, Object[] args) {

换句话说,它需要一个对象数组。从 Java 5 开始,该方法声明为

public PrintStream format(String format, Object... args) {

使用varargs syntax 。您使用的编译器版本可能低于 Java 5。

关于java - 我需要使这段代码正常工作,但无法找出问题所在。这是一个 System.out.format() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com