我无法以 Java 中的长数据类型打印 Math 类的结果?

我正在尝试创建一个 n 从 16 到 2048 的表以及 log(n)、n*log(n)...


首先,我用 double 对结果进行了 printf 编辑,结果很好,除了表格没有对齐,而是在 n = 256 处搞乱了。我认为结果对于 double 数据来说可能太大了类型所以我切换到长。


public class FunctionGrowth {

    public static void main(String[] args) {


        long n = 16L;

        System.out.println("log(n) \tn \t\tn*log(n)\t\tn^2 \tn^3 \t\t2^n");


        while(n <= 2048) {

            long l = (long) Math.log(n);

            long nl = (long) (n*Math.log(n));

            long pow = (long) Math.pow(n,2);

            long cube = (long) Math.pow(n,3);

            long pow2 =(long) Math.pow(2,n);


            System.out.printf("%.2d\t%.0d\t\t%.2d\t\t\t%d\t%d\t\t%d\n", l, n, nl, pow, cube, pow2);

            n = n*2;

        }

    }

}

但现在我有一个新问题: Exception in thread "main" java.util.IllegalFormatPrecisionException: 2


我尝试了很多方法将 log(n), pow(n,2)... 从 Math 类转换为 long,但到目前为止没有任何效果。


你能帮我解决这个问题吗?我很挣扎。


开心每一天1111
浏览 147回答 4
4回答

繁花不似锦

您遇到的行为是由于指定整数类型的精度而导致的,该精度无效。此行为在类 Javadoc 中指定Formatter:精确对于一般参数类型,精度是要写入输出的最大字符数。对于浮点转换“a”、“A”、“e”、“E”和“f”,精度是小数点后的位数。如果转换为“g”或“G”,则精度是四舍五入后结果大小的总位数。对于字符、整数和日期/时间参数类型以及百分比和行分隔符转换,精度不适用;如果提供了精度,则会抛出异常。

暮色呼如

不建议这样做,但您可以采用这样的方法。public static void main(String[] args) {&nbsp; &nbsp; long n = 16L;&nbsp; &nbsp; // Do use format number accordingly&nbsp; &nbsp; int format = 20;&nbsp; &nbsp; System.out.printf(&nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\t"+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "s\n", "log(n)", "(n)", "nlog(n)", "n^2", "n^3", "2^n");&nbsp; &nbsp; //System.out.println("log(n) \tn \t\tn*log(n)\t\tn^2 \tn^3 \t\t2^n");&nbsp; &nbsp; while (n <= 2048) {&nbsp; &nbsp; &nbsp; &nbsp; long l = (long) Math.log(n);&nbsp; &nbsp; &nbsp; &nbsp; long nl = (long) (n * Math.log(n));&nbsp; &nbsp; &nbsp; &nbsp; long pow = (long) Math.pow(n, 2);&nbsp; &nbsp; &nbsp; &nbsp; long cube = (long) Math.pow(n, 3);&nbsp; &nbsp; &nbsp; &nbsp; long pow2 = (long) Math.pow(2, n);&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\t"+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\t" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "%" + format + "d\n", l, n, nl, pow, cube, pow2);&nbsp; &nbsp; &nbsp; &nbsp; n = n * 2;&nbsp; &nbsp; }}

慕桂英4014372

您收到的错误是因为您将 long 值格式化为 int 值,而 int 不支持精度格式化。尝试将长值转换为浮点数,并将格式说明符(%.2d 和类似的)更改为浮点说明符(%.2f 和类似的)。

呼唤远方

您可以使用 DecimalFormat:public static void main(String[] args) {&nbsp; &nbsp; long n = 16L;&nbsp; &nbsp; System.out.println("log(n) \tn \t\tn*log(n)\t\tn^2 \tn^3 \t\t2^n");&nbsp; &nbsp; while(n <= 2048) {&nbsp; &nbsp; &nbsp; &nbsp; long l = (long) Math.log(n);&nbsp; &nbsp; &nbsp; &nbsp; long nl = (long) (n*Math.log(n));&nbsp; &nbsp; &nbsp; &nbsp; long pow = (long) Math.pow(n,2);&nbsp; &nbsp; &nbsp; &nbsp; long cube = (long) Math.pow(n,3);&nbsp; &nbsp; &nbsp; &nbsp; long pow2 =(long) Math.pow(2,n);&nbsp; &nbsp; &nbsp; &nbsp; DecimalFormat df = new DecimalFormat("0.00");&nbsp; &nbsp; &nbsp; &nbsp; String format = df.format(l) + "\t" +&nbsp; df.format((nl)) + "\t" + df.format(pow) + "\t" + df.format(cube) + "\t" + df.format(pow2);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(format);&nbsp; &nbsp; &nbsp; &nbsp; n = n*2;&nbsp; &nbsp; }}您可以在此处了解有关 DecimalFormat 的更多信息:https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java