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

问题描述 投票:-2回答:4

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

起初,我将结果打印成两倍,但结果很好,除了表格未对齐,而是在n = 256处弄乱了。我认为结果对于双重数据类型,所以我切换到long。

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;
        }
    }
}

但现在我遇到了一个新问题:线程“主”中的异常java.util.IllegalFormatPrecisionException:2

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

您能帮我解决这个问题吗?我很努力。

java exception long-integer
4个回答
0
投票

您遇到的行为是由于为整数类型指定了精度,这是无效的。

此行为在Formatter类Javadoc中指定:

精度

对于一般参数类型,精度是要写入输出的最大字符数。

对于浮点转换'a','A','e','E'和'f',精度是小数点后的位数。如果转换是“ g”或“ G”,则精度是四舍五入后得到的幅度中的位数总数。

对于字符,整数,以及日期/时间参数类型以及百分比和行分隔符的转换,精度不适用;如果提供精度,则将引发异常


0
投票

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

谢谢


0
投票

您可以使用DecimalFormat:

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);

        DecimalFormat df = new DecimalFormat("0.00");
        String format = df.format(l) + "\t" +  df.format((nl)) + "\t" + df.format(pow) + "\t" + df.format(cube) + "\t" + df.format(pow2);
        System.out.println(format);
        n = n*2;
    }
}

您在这里有有关DecimalFormat的更多信息:

https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html


0
投票

不推荐这样做,但是您可以采用这种方法。

public static void main(String[] args) {

    long n = 16L;

    // Do use format number accordingly
    int format = 20;

    System.out.printf(
        "%" + format + "s\t" +
            "%" + format + "s\t" +
            "%" + format + "s\t" +
            "%" + format + "s\t"+
            "%" + format + "s\t" +
            "%" + format + "s\n", "log(n)", "(n)", "nlog(n)", "n^2", "n^3", "2^n");

    //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(
            "%" + format + "d\t" +
            "%" + format + "d\t" +
            "%" + format + "d\t" +
            "%" + format + "d\t"+
            "%" + format + "d\t" +
            "%" + format + "d\n", l, n, nl, pow, cube, pow2);
        n = n * 2;
    }
}

OutPut for the above snippet

© www.soinside.com 2019 - 2024. All rights reserved.