gpt4 book ai didi

java - 在java中使用printf遇到困难并且在eclipse中读取错误

转载 作者:行者123 更新时间:2023-12-02 07:32:18 24 4
gpt4 key购买 nike

public class TryMyMath {

public static void main(String args[])
{

double num = Math.E;
printTable(num);
}// end main

public static void printTable(double num){
int n = 1;

for(n = 1;n <= 10; n++)
{
num = Math.pow(num, n);
System.out.printf("%d %lf", n, num);
}
round100th(num);

}
public static double round100th(double num)
{
return Math.round(num*100.)/100.0 ;
} // end round100th

}

我对在java中使用printf有这个问题,所以这是我使用printf时遇到的错误

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'l'
at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at TryMyMath.printTable(TryMyMath.java:15)
at TryMyMath.main(TryMyMath.java:7)

当我取出 printf 语句时没有任何错误,我是否没有按照 java 中应该使用的正确方式使用 printf ?

而且我是 java 和 eclipse 的新手,所以我不知道这些错误是什么意思。

这就是我得到的输出

1 2.71828227.3890563403.4287934 26489122129.8434375 13041808783936237000000000000000000000000000000000000.0000006 无限7无限8 无限9无限10无穷大

输出仅在 n = 3 之前正确,我没有发现 for 循环有任何问题,为什么会发生这种情况?

nvm 我发现它出了什么问题

最佳答案

应该是System.out.printf("%d %f", n, num)l这里没有意义。

此外,您的round100th(num);调用没有效果 - 您是否尝试将数字打印到小数点后两位?您可以通过以下方式做到这一点:

System.out.printf("%d %.2f", n, num)

关于java - 在java中使用printf遇到困难并且在eclipse中读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12734954/

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