简体   繁体   English

二维数组

[英]Two dimensional array

I am working on a little program to calculate and draw a parabola. 我正在研究一个用于计算和绘制抛物线的小程序。 But I'm stuck at a little part in the program where I need to calculate the valuetable. 但是我只在程序中需要计算价值表的一小部分。

The quadratic function is the following one: y = a * (x - alpha)² + Beta . 二次函数为以下函数: y = a * (x - alpha)² + Beta

So if I fill in the following values for this function: y = 2 * (x - 1)² + (-12) . 因此,如果我为此函数填写以下值: y = 2 * (x - 1)² + (-12) I need to become this value table: 我需要成为此值表:

x | x | -4.0 | -4.0 | -3.0 | -3.0 | -2.0 | -2.0 | -1.0 | -1.0 | 0.0 | 0.0 | 1.0 | 1.0 | 2.0 | 2.0 | 3.0 | 3.0 | 4.0 | 4.0 |

y | y | 38.0 | 38.0 | 20.0 | 20.0 | 6.0 | 6.0 | -4.0 |-10.0|-12.0|-10.0|-4.0 | -4.0 | -10.0 | -12.0 | -10.0 | -4.0 | 6.0 | 6.0 |

I managed to get the first row right, but the second row (the one that calculates te values), is totally wrong. 我设法使第一行正确,但是第二行(用于计算te值的行)完全错误。

    public double[][] berekenWaardentabel(int input[], int AANTALKOLOMMEN, double startWaarde) {
    double[][] waarden = new double[RIJEN][AANTALKOLOMMEN];

    System.out.println(waarden.length);
    System.out.println(startWaarde + "\n");

    for(int i = 0; i < RIJEN; i++) {
        for(int j = 0; j < AANTALKOLOMMEN; j++) {
            waarden[0][j] = startWaarde++; //value of first row (start from -4.0, counts upwards) 
            waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];
            System.out.print(waarden[i][j] + "   ");
        }
        System.out.println();
    }
    return waarden;
}

This is the output I get: 这是我得到的输出:

-4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 -4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0

86.0 | 86.0 | 116.0 | 116.0 | 150.0 | 150.0 | 188.0 | 188.0 | 230.0 | 230.0 | 276.0 | 276.0 | 326.0 | 326.0 | 380.0 | 380.0 | 438.0 | 438.0 | 500.0 | 500.0 | 566.0 566.0

Anyone an idea how to solve this problem? 有人知道如何解决这个问题吗? Thanks! 谢谢!

You have to mistakes : 你要犯错误:

you want to fill two lines of your array (the 0-th (x) and the first(y)). 您想填充数组的两行(第0(x)和第一个(y))。 Then you store the value of x in the first row and compute y(x+1) in the second row. 然后,将x的值存储在第一行中,并在第二行中计算y(x + 1)。

you need to fill your array with : 您需要使用以下填充数组:

for (int j = 0; j < AANTALKOLOMMEN; j++) {
    waarden[0][j] = startWaarde; // value of first row (start from -4.0, counts upwards)
    waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];
    startWaarde++;
}

Then you can display your values : 然后,您可以显示您的值:

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < AANTALKOLOMMEN; j++) {
        System.out.print(waarden[i][j] + "   ");
    }
    System.out.println();
}

First error is this: 第一个错误是这样的:

        waarden[0][j] = startWaarde++; //value of first row (start from -4.0, counts upwards) 
        waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];

You put the value of startWaarde in waarden[0][j] . 您将startWaarde的值放在waarden[0][j] And then you increment it by one. 然后将其递增一。

Then you calculate the value of the function for the updated startWaarde . 然后,为更新的 startWaarde计算函数的值。

So your waarden[0][j] is, perhaps, 4.0 , but you calculate the value waarden[1][j] for 5.0 rather than 4.0 . 因此,您的waarden[0][j]可能是4.0 ,但是您为5.0而不是4.0计算了waarden[1][j]的值。

Your other problem is that you are looping with this line: 您的另一个问题是您正在使用以下行:

for(int i = 0; i < RIJEN; i++) {

I don't know what the value of RIJEN is. 我不知道RIJEN的价值是什么。 But your waarden array is only supposed to have two rows. 但是您的waarden数组仅应具有两行。 Once you go through the loop again and again, startWaarde keeps growing and growing. 一旦您一次又一次地循环, startWaarde不断增长。 Because you print the second row only after this loop iterates the second time, you see the wrong values. 因为仅在此循环第二次迭代之后才打印第二行,所以您看到了错误的值。

You shouldn't have this loop at all. 您根本不应该有此循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM