Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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
如何从C文件函数(Java本机接口)仅调用Java的返回值_Java_C_Methods_Java Native Interface_Return - Fatal编程技术网

如何从C文件函数(Java本机接口)仅调用Java的返回值

如何从C文件函数(Java本机接口)仅调用Java的返回值,java,c,methods,java-native-interface,return,Java,C,Methods,Java Native Interface,Return,我有一个Java文件,它声明了在我的C文件中定义的本机方法average()。该方法取两个数字的平均值,打印“在C中,数字是n1,n2”(n1和n2是输入数字),并以双精度返回平均值 Java文件然后调用average()方法,打印“在Java中,平均值为(n1+n2)/2”,然后打印平均值x2 下面是一个数字为3和2的示例(运行我的Java文件): 在C中,数字是3和2 在Java中,平均值为2.5 在C中,数字是3和2 5.0 我只想将C文件的返回值(平均值)乘以2。我不想再次打印字符串“在

我有一个Java文件,它声明了在我的C文件中定义的本机方法average()。该方法取两个数字的平均值,打印“在C中,数字是n1,n2”(n1和n2是输入数字),并以双精度返回平均值

Java文件然后调用average()方法,打印“在Java中,平均值为(n1+n2)/2”,然后打印平均值x2

下面是一个数字为3和2的示例(运行我的Java文件):

在C中,数字是3和2

在Java中,平均值为2.5

在C中,数字是3和2

5.0

我只想将C文件的返回值(平均值)乘以2。我不想再次打印字符串“在C中,数字是3和2”。如何只打印C文件中的返回值(平均值),而不再次打印字符串“在C中,数字是3和2”?我知道我可以创建另一种方法,但这将是非常重复的

我的Java文件如下:

public class TestJNIPrimitive {
    
    static {
        System.loadLibrary("myjni"); //myjni.dll (Windows)
    }

    private native double average(int n1, int n2);
    public static void main(String args[]) {
        
        System.out.println("In Java, the average is " + new TestJNIPrimitive().average(3,2));
        double result = new TestJNIPrimitive().average(3,2);
        System.out.println(result*2);
    }
}
JNIEXPORT jdouble JNICALL Java_TestJNIPrimitive_average(JNIEnv *env, jobject thisObj, jint n1, jint n2) {
    
    printf("In C, the numbers are %d and %d\n", n1, n2);
    jdouble result;
    result = ((jdouble)n1 + n2) / 2.0;
    // jint is mapped to int, jdouble is mapped to double
    return result;
}
我的C文件如下:

public class TestJNIPrimitive {
    
    static {
        System.loadLibrary("myjni"); //myjni.dll (Windows)
    }

    private native double average(int n1, int n2);
    public static void main(String args[]) {
        
        System.out.println("In Java, the average is " + new TestJNIPrimitive().average(3,2));
        double result = new TestJNIPrimitive().average(3,2);
        System.out.println(result*2);
    }
}
JNIEXPORT jdouble JNICALL Java_TestJNIPrimitive_average(JNIEnv *env, jobject thisObj, jint n1, jint n2) {
    
    printf("In C, the numbers are %d and %d\n", n1, n2);
    jdouble result;
    result = ((jdouble)n1 + n2) / 2.0;
    // jint is mapped to int, jdouble is mapped to double
    return result;
}

谢谢你的帮助

除非我遗漏了什么,否则您不能两次调用C
average
函数,只需在调用一次后存储结果:

public static void main(String args[]) {
    double result = new TestJNIPrimitive().average(3,2);
    System.out.println("In Java, the average is " + result);
    System.out.println(result*2);
}

除非我遗漏了什么,否则您不能两次调用C
average
函数,只需在调用一次后存储结果:

public static void main(String args[]) {
    double result = new TestJNIPrimitive().average(3,2);
    System.out.println("In Java, the average is " + result);
    System.out.println(result*2);
}

添加第三个参数怎么样?(允许禁止打印该行)
newtestjniprimitive().average(3,2)调用您的函数。你调用它两次,它就会被调用两次。添加第三个参数怎么样?(允许禁止打印该行)
newtestjniprimitive().average(3,2)调用您的函数。你叫它两次,它就会被叫两次。