0

I am required to convert several dozen mathematical functions into C programs, and then into Java equivalent. I'm not that great in Java so how would we call the following function double mvfBeale(int n, double *x) in Java. Is a dynamic array the way to go for variable x

Following is a simple program in c, for which I need the Java equivalent to get started.

#include<stdio.h>
#include<math.h>


double mvfBeale(int n, double *x)
{
    return pow(1.5 - x[0] + x[0]*x[1], 2) +
    pow(2.25 - x[0] + x[0] * x[1]*x[1], 2) +
    pow(2.625 - x[0] + x[0] * pow(x[1], 3), 2);
}


int main(void)
{
    int n;
    double x;
    double result;

    printf("Enter n: ");
    scanf("%d", &n);

    printf("Enter x: ");
    scanf("%lf", &x);

    result = mvfBeale(n, &x);
    printf("Beale = %lf", result);

}

Thanks in advance for your guidance.

4

1 回答 1

4

在 Java 中,您需要将独立的 C 函数包装在一个类中。您需要声明这些函数static

public class MathHelper {
    public static double mvfBeale(int n, double[] x)
    {
        return Math.pow(1.5 - x[0] + x[0]*x[1], 2) +
        Math.pow(2.25 - x[0] + x[0] * x[1]*x[1], 2) +
        Math.pow(2.625 - x[0] + x[0] * Math.pow(x[1], 3), 2);
    }
}

请注意,由于pow它是 C 中的独立函数,因此其 Java 版本需要将其称为Math类的成员。

于 2013-07-22T17:16:16.733 回答