我正在制作一种绘图方法,y 值不断翻转

I am making a graphing method and the y-Value keeps getting flipped

提问人:Mikey 提问时间:4/3/2023 最后编辑:Mikey 更新时间:4/3/2023 访问量:23

问:

因此,每当我输入值“1.0, 0.0”或值“1.0, 2.0”时,我制作的图形都会返回一个翻转的 y 值,而每当我输入值 1.0、0.0 时,图形就会完全偏离 5。这种方法旨在接受多项式中的双精度值(系数)的数组列表,然后求解 y,将 -5 到 5 的值代入方程。图形输出,x 值是正确的,它只是翻转的 y 值。

那么,到目前为止,我尝试了什么?好吧,在 3 个不同的地方否定 yVal,在 for 循环中否定 xVal,分离 for 循环,统一它,使用不同的变量,一切。没有任何效果,我花了大约 2 个小时。ATP我认为这可能是一些简单的东西,我只是看不见,但我真的无法弄清楚它是什么。我也认为这可能是我的替代方法关闭了,但我已经测试了很多,没有任何问题。测试用例:“1.0、0.0”、“1.0、2.0” 法典: ```

public static double sub(ArrayList<Double> currentCoefList, double val) {
    double subbed = 0.0;
    int i = 0;
    for (i = 0; i < currentCoefList.size() - 1; i++) {
        subbed += Math.pow(currentCoefList.get(i) * val, currentCoefList.size() - i - 1);
    }
    subbed = subbed + currentCoefList.get(i);
    return subbed;
}

public static char[][] makesGraph(ArrayList<Double> currentArrayList) {

    int row = 11;
    int col = 11;
    int xVal = -5;
    int yVal = 0;
    int[] ysVal = new int[11];
    char[][] grid = new char[row][col];

    for (int r = 0; r < grid.length; r++) {
        for (int c = 0; c < grid[0].length; c++) {
            if (r == 5 && c == 5) {
                grid[r][c] = '+';
            } else if (c == 5) {
                grid[r][c] = '|';
            } else if (r == 5) {
                grid[r][c] = '-';
            } else {
                grid[r][c] = ' ';
            }
        }
    }

    for (int i = 0; i < grid[0].length; i++){
        ysVal[i] = (int) sub(currentArrayList, xVal);
        xVal++;
    }

    for (int i = 0; i < grid[0].length; i++) {
        yVal = Math.round(ysVal[i]);
        if(yVal < 0)
        {
            yVal = yVal + 5;
        }
        else if (i < row && yVal < col) {
            grid[i][yVal] = '*';
        }
    }

    for (int r = 0; r < grid.length; r++) {
        for (int c = 0; c < grid[0].length; c++) {
            System.out.print(grid[r][c]);
        }
        System.out.println();
    }

    return grid;
}
Java Math ArrayList 方法

评论


答:

0赞 ruakh 4/3/2023 #1

看起来您的方法旨在计算 0 x n + a 1 x n−1 + ... +an−1 x + a n; 我有这个权利吗?如果是这样,那么——这不是你所实现的。你把你的系数 a i 放在对 的调用,所以你计算的是 (a i x)n−i 而不是 ix n−i)。subMath.pow

可能还有其他问题;这只是一个在试图弄清楚你想做什么时突然跳出来的人。 Eric Lippert 写了一篇非常有用的博客文章,“如何调试小程序”,可以帮助您找到并修复它们。:-)