You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java随机密码猜解程序的密码比对与尝试次数逻辑问题

密码猜解程序比对逻辑修复方案

原代码存在的核心问题

  • 执行顺序颠倒:先接收用户第一次猜解输入,后生成随机密码,基础逻辑不成立
  • 密码字符集错误:定义的字母串每个字符间带空格,会导致生成的密码包含无意义空格,用户无法正常猜解
  • 长度计算冗余:手动写循环统计输入字符串长度,可直接调用String.length()方法实现
  • 校验逻辑残缺:仅做长度判断,没有实现密码内容完全匹配的校验,且3次尝试循环内没有更新输入内容的长度值,长度校验逻辑完全失效
  • 尝试计数逻辑错误:没有对错误尝试(长度错误/内容错误)做统一计数,无法触发3次失败后公布正确密码的逻辑

修复实现

Java中字符串的完全相等校验直接调用String.equals()方法即可,该方法会同时校验两个字符串的长度、每一位字符是否完全匹配,无需手动编写循环逐位对比。
修复后完整可运行代码如下:

import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;

public class PasswordGuess {
    final static Scanner in = new Scanner(System.in);

    public static void method(int len) {
        // 修正字符集,去掉多余空格,仅保留大小写字母
        String ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        SecureRandom sr = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        
        // 先生成目标随机密码
        for(int i = 0; i < len; i++){
            int rand = sr.nextInt(ASCII.length());
            sb.append(ASCII.charAt(rand));
        }
        String correctPwd = sb.toString();

        int failCount = 0;
        System.out.println("Guess the generated password");
        while (failCount < 3) {
            String userInput = in.nextLine();
            // 先校验长度
            if (userInput.length() != len) {
                failCount++;
                System.out.printf("The length of the random generated password is %d and the length of the password inserted is %d%n", len, userInput.length());
                if (failCount < 3) {
                    System.out.println("The length of the generated password and the length of the inserted password varies. Please try again...");
                    System.out.println("Guess the generated password again");
                }
                continue;
            }
            // 长度匹配后校验内容完全一致
            if (userInput.equals(correctPwd)) {
                System.out.println("Congratulations! You guessed the password correctly.");
                return;
            } else {
                failCount++;
                if (failCount < 3) {
                    System.out.println("Password mismatch, please try again.");
                    System.out.println("Guess the generated password again");
                }
            }
        }
        // 3次失败后输出正确密码
        System.out.printf("You have failed 3 attempts, the correct password is: %s%n", correctPwd);
    }

    public static void main(String[] args) {
        Random r = new Random();
        // 生成1-7位长度的随机密码
        int len = r.nextInt(1, 8);
        System.out.println("length :" + len);
        method(len);
    }
}

逻辑说明

  • 调整执行顺序:程序启动后先生成符合长度要求的随机密码,再启动猜解流程
  • 统一错误计数:无论是长度不匹配还是内容不匹配,都记为1次失败尝试
  • 分层校验:先校验长度,长度不匹配直接提示无需比对内容,提升运行效率;长度匹配后调用equals()方法做全内容比对
  • 终止逻辑:用户猜对直接输出成功提示终止方法;累计3次失败后直接输出正确密码,结束流程

内容的提问来源于stack exchange,提问作者Qwadwo Richmond

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 15:39:13