Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Math.pow()显示精度下降?_Java_Loops - Fatal编程技术网

Java 为什么Math.pow()显示精度下降?

Java 为什么Math.pow()显示精度下降?,java,loops,Java,Loops,为什么会出现显示精度损失的错误,尽管hackerrank中的答案仅为整数?当我使用double时,它会像2.0,5.0那样打印出来,与输出不匹配 public class Solution { public static void main(String args[]) throws IOException { /* enter your code here. Read input from STDIN. Print output to STDOUT. Your cla

为什么会出现显示精度损失的错误,尽管hackerrank中的答案仅为整数?当我使用double时,它会像2.0,5.0那样打印出来,与输出不匹配

public class Solution {

    public static void main(String args[]) throws IOException {
        /* enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        int x, y;
        int p = 2;
        int q = 0;
        int i, j;
        for(j = 0; j < t; j++){
            y = 0;
            x = 0;
            int a = s.nextInt();
            int b = s.nextInt();
            int n  = s.nextInt();
            x = (a + Math.pow(p, q) * b);
            System.out.print(x);

            for(i = 1; i < n; i++) {
                y = (x + Math.pow(p, i) * b);
                System.out.print(y);
                x = y;
            }
            System.out.println();
        }
    }
}
当你确定p和i都是整数时,你就可以进行数学运算了。。。调用Integer。您正在通过向通话中添加int来进行转换:

a+intMath.powp,q*b


第二次调用Math.pow也是如此…

提示:Math.pow的返回类型是什么?我已经格式化了您的代码,以确保可读性,因为发布时您的代码格式很差。今后我强烈建议你自己做这件事。格式设置非常重要,因为如果您的代码不是标准的可接受格式,则可读性不强,如果不可读,则不可理解,这可能会降低您获得及时正确答案的可能性。Math.pow的返回类型是指定给int变量x的两倍。