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

密码管理器网站选择列表编号异常及访问失败问题求助

密码管理器代码问题修复方案

问题1:网站编号持续递增

原因

websiteNum变量在循环外部初始化,每次进入while (yesNo == 'n')循环时未重置,导致每次打印列表都会在上次的数值基础上累加。

修复方法

取消websiteNum这个额外变量,直接用循环变量i的i+1作为网站编号(列表索引从0开始,编号需从1起始),这样每次打印列表都会从1重新计数。


问题2:输入任何编号都提示无法访问

原因

判断条件userNum == websiteNum逻辑错误:打印完列表后,websiteNum的值已等于列表总长度+1(比如2个网站,打印后websiteNum为3),而用户输入的编号是1或2,永远无法匹配。另外while (alwaysTrue == true)循环完全冗余,只会执行一次就break。

修复方法

直接检查用户输入的userNum是否在有效范围(1到网站列表长度之间),合法则通过j = userNum -1取对应索引的内容,否则提示错误。


修改后的代码片段

//PASSWORD Y/N LOOP
//Does not want to add password, will list stores websites and corresponding numbers
while (yesNo == 'n') {
    //WEBSITE LIST MENU LOOP
    // 直接用i+1作为编号,无需额外变量websiteNum
    for (i = 0; i < websites.getwebList().size(); i++) {
        System.out.print((i + 1) + " - ");
        System.out.println(websites.getwebList().get(i));
    }
    //Asks for user input of number of website they want to access
    System.out.println("Enter the number of the website you want to access:");
    userNum = scnr.nextInt();
    // 检查输入是否合法
    if (userNum >= 1 && userNum <= websites.getwebList().size()) {
        j = userNum - 1;
        System.out.println(websites.getwebList().get(j));
        System.out.println(usernames.getuserNameList().get(j));
        System.out.println(passwords.getPassList().get(j));
    } else {
        System.out.println("Cannot access, try again.");
    }
}

内容的提问来源于stack exchange,提问作者Kaiya Smallwood

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 19:50:23