给定计算的程序输出错误

时间:2019-08-10 21:05:44

标签: java

任何人都可以协助我检查代码为什么没有给我正确的答案或我做错了什么地方。多谢您的帮助。
另外,下面显示了我尝试实现的计算和代码。提前致谢。

扇形度量的计算如下所示

提供数据Si = { 0.2554, 0.4464 , 0.3897 , 4.2112} 迭代每个实例的值

========================================================
Sj1 = 0.2554                                 Si1 = {0.2554}
Si1 > Sj1
0.2554 > 0.2554 = 0
Si1 = Sj2
0.2554 = 0.2554 = 1
Fand
(0   +   (0.5 * 1) )/ 1  =  0.5
==========================================================
Sj2 = 4.4464                                 Si2 = {0.2554, 4.4464}
Si2 > Sj2
0.2554 > 4.4464 = 0
4.4464 > 4.4464 = 0
Si2 = Sj2
0.2554 = 4.4464 = 0
4.4464 = 4.4464 = 1
Fand
( 0   +  (0.5 * 1) )/ 2  =  0.25
====================================================
Sj3 = 0.3897                               Si3 = {0.2554, 4.4464, 0.3897}
Si3 > Sj3
0.2554 > 0.3897 = 0
4.4464> 0.3897 = 1
0.3897 > 0.3897 = 0
Si3 = Sj3
0.2554 = 0.2554 = 0
4.4464 = 0.3897 = 0
0.3897 = 0.3897 = 1
Fand
( 1  +  (0.5 * 1) )/ 3  =  0.5  = 0.5
===========================================================
Sj4 = 4.2112                   Si4 = {0.2554, 0.4464, 0.3897, 0.42112}
Si4  > Sj4
0.2554 > 4.2112 = 0
0.4464 > 4.2112 = 1
0.3897 > 4.2112 = 0
0.42112 > 4.2112 = 0
Si4  = Sj4
0.2554 = 4.2112 = 0
0.4464 = 4.2112 = 0
0.3897 = 4.2112 = 0
0.42112 = 4.2112 = 1
Fand
( 1   +  (0.5 * 1) )/ 4  =  0.5 = 0.375
======================================================
Result
0.5, 0.25, 0.5, 0.375
The Fand formular in matlab code is
value = (sum( a(1:i) > a(i) ) + 0.5 * sum ( a(i) == a(1:i) ) ))/i
The Fand normal formular
Fand = ( #(si > sj) + 0.5 (si = sj)))/ i
======================================================

请在下面找到我的Java类Madion:

class madion {
    public static void main(String[] args) {
        double data[] = {0.2554, 4.4464, 0.3897, 4.2112};
        //initialise variables
        double sam = 0;
        double sam1 = 0;
        int i;
        int j = 0;
        double m = 0;
        double n = 0;
        int count = 0;
        int count1 = 0;
        //use for loop to iterate through the arrays
        for (i = 0; i < data.length; i++) {
            n = data[i];
            for (j = 0; j <= i; j++) {
                m = data[j];
                count = 0;
                //use if statement to express the conditions and counts
                if (n == m) {
                    count++;
                } else if (m > n) {
                    count1++;
                }
            }
            double fand = 0;
            for (i = 1; i <= data.length; i++) {
                // System.out.println(i );
                fand = ((count1) + (0.5 * count)) / i;
                System.out.println(fand);
                //  System.out.println(count);
                //   System.out.println(count1);
            }
        }
    }
}

我希望输出为0.5, 0.25, 0.5, 0.375,但实际上是

0.5, 0.25, 0.1666, 0.125

2 个答案:

答案 0 :(得分:0)

Matlab解决方案中有三件事是不同的。

  1. 您要除以的i在Java中从0开始,在Matlab中从1开始
  2. 里面有一个额外的循环,可以操纵i值
  3. 在不同级别重置计数

代码应如下所示:

class madion {
    public static void main(String[] args) {
        double data[] = {0.2554, 4.4464, 0.3897, 4.2112};

        //use for loop to iterate through the arrays
        for (int i = 0; i < data.length; i++) {
            double n = data[i];
            int count = 0;
            int count1 = 0;
            for (int j = 0; j <= i; j++) {
                double m = data[j];
                //use if statement to express the conditions and counts
                if (n == m) {
                    count++;
                }
                if (m > n) {
                    count1++;
                }
            }
            double fand = ((count1) + (0.5 * count)) / (i+1);
            System.out.println(fand);
        }
    }
}

答案 1 :(得分:0)

首先,您应该正确命名您的变量,以允许另一个人 更快地理解您的代码,然后

  • 删除最后一个无用的循环,该循环实际上是使用i作为索引,因此外循环仅执行一个循环
  • 在每次迭代时重置countEqcountSup
  • 除以n+1作为数组索引的0起始点
public static void main(String[] args) {
    double[] data = {0.2554, 4.4464, 0.3897, 4.2112};
    double checkVal, currentVal;
    int countEq, countSup;

    //use for loop to iterate through the arrays
    for (int i = 0; i < data.length; i++) {
        countEq = countSup = 0;
        currentVal = data[i];
        for (int j = 0; j <= i; j++) {
            checkVal = data[j];
            if (currentVal == checkVal) {
                countEq++;
            } else if (checkVal > currentVal) {
                countSup++;
            }
        }
        double fand = (countSup + 0.5 * countEq) / (i + 1);
        System.out.println(currentVal + " " + fand);
    }
}