【问题标题】:I'm trying to write a program to test the accuracy percentage of 8 sets of security codes我正在尝试编写一个程序来测试8组安全码的准确率
【发布时间】:2021-09-09 07:07:48
【问题描述】:

我需要帮助编写一个程序,下面必须测试 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% 准确的”

【问题讨论】:

  • 准确率是通过匹配项除以数组中的总项计算得出的吗?
  • 好的,这样我们就可以得到百分比的答案
  • 您需要哪些具体帮助?你试过什么?它做了什么不符合你的预期?你做了什么来尝试解决这个问题?
  • 我已经在 compare() ``` public static void compare(){ int[] code = {6,1,1,6,3,1,4,7,7 ,1};布尔测试=假; for(int element : code){ if(elemet == security_codes) test = true;休息; } }```

标签: java arrays algorithm data-structures


【解决方案1】:
  • 遍历每个数组

  • 在每个循环中,将匹配的数量存储在一个变量中,并在当前循环通过的数字等于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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多