我正在尝试编写一个程序来测试 8 组安全代码的准确率

时间:2021-06-26 02:01:20

标签: java arrays algorithm data-structures

我需要帮助编写一个程序,必须测试 8 组安全代码的准确率,每组包含 10 个 java(数据结构)中的整数值


public class lab_three_solution {
    /* Solution method: Complete this method only. Also add a relevant parameter to this method*/
    
    public static void compare(){
              
    }

    /* Main method: Pass the Security Codes as an argument when calling the 'compare' method */
    public static void main(String args[]) {

        // 8 Security Codes to compare
        int[][] security_codes = {{0,1,1,7,7,2,2,5,0,3}, {5,1,4,0,5,0,9,8,7,5}, {9,8,3,4,0,9,6,7,7,1},
                {5,9,5,7,1,4,9,7,6,9}, {7,1,1,4,6,7,9,1,1,0}, {6,1,1,6,3,1,4,7,7,1}, {6,1,1,8,4,9,7,0,1,2},
                {9,5,4,6,3,1,4,7,2,9}};

        compare(security_codes);
    }
}

通过将 8 个代码中的每个代码的序列与 正确代码顺序如下

正确 = {6,1,1,6,3,1,4,7,7,1}

我必须在 JAVA 中设计和实现一个算法,将精度打印为 通过将 8 个安全代码中的每一个与 1 个可接受的代码进行比较,得出一个百分比。即如果 说安全代码 6 序列匹配可接受代码的序列然后我的输出 应该是“安全代码 1 是 100% 准确的”

1 个答案:

答案 0 :(得分:0)

  • 遍历每个数组

  • 在每个循环中,将匹配的数量存储在一个变量中,并在当前循环通过的数字等于 correct 数组中相同索引处的数字时增加该变量

  • 通过将匹配项除以数组的长度来计算准确度

static final int[] correct = {6,1,1,6,3,1,4,7,7,1};
public static void compare(int[][] a){
    for(int[] b : a) {
        int matches = 0;
        for(int i = 0; i < b.length; i++) {
            matches += correct[i] == b[i] ? 1 : 0;
        }
        double accuracy = matches * 1.0 / b.length;
        System.out.println(accuracy);
    }
}
public static void main(String[] args) {
    int[][] security_codes = {
            {0,1,1,7,7,2,2,5,0,3},
            {5,1,4,0,5,0,9,8,7,5},
            {9,8,3,4,0,9,6,7,7,1},
            {5,9,5,7,1,4,9,7,6,9},
            {7,1,1,4,6,7,9,1,1,0},
            {6,1,1,6,3,1,4,7,7,1},
            {6,1,1,8,4,9,7,0,1,2},
            {9,5,4,6,3,1,4,7,2,9}};
    compare(security_codes);
}

结果:

0.2
0.2
0.3
0.1
0.2
1.0
0.3
0.5
相关问题