Java编写程序计算8组安全码与正确序列比对准确率求助
Java安全码匹配准确率计算实现
你只需按如下代码修改compare方法即可,不需要调整其他代码部分:
// 首先为方法添加二维int数组入参接收安全码集合 public static void compare(int[][] security_codes){ // 定义正确的安全码序列 int[] correct = {6,1,1,6,3,1,4,7,7,1}; int totalLength = correct.length; // 遍历每一组待校验的安全码 for (int i = 0; i < security_codes.length; i++) { int matchCount = 0; // 按位比对 for (int j = 0; j < totalLength; j++) { if (security_codes[i][j] == correct[j]) { matchCount++; } } // 计算准确率百分比 int accuracy = (matchCount * 100) / totalLength; // 按要求格式输出,注意安全码编号从1开始计数 System.out.printf("Security Code %d is %d%% accurate\n", i+1, accuracy); } }
实现逻辑说明
- 首先给
compare方法添加int[][] security_codes入参,接收主方法传入的8组安全码 - 内部预定义官方给出的正确安全码序列,按外层循环遍历每组待校验安全码
- 内层循环按位对比待校验码和正确码的对应位置值,统计匹配的位数
- 准确率计算规则为 (匹配位数 / 总位数) 100%,按要求格式输出每组的校验结果
运行后会直接输出8组安全码的匹配准确率,其中第6组会输出Security Code 6 is 100% accurate,和示例要求一致。
内容的提问来源于stack exchange,提问作者Baraka Danny
相关产品推荐
相关产品推荐

