topcode【随机算法题】【2026.5.24打卡-java版本】
2026/5/25 7:34:20
package com.lkbhua.Test2; import java.io.*; import java.util.Scanner; public class test1 { public static void main(String[] args) throws IOException { /* 需求: 写一个登入小案例 步骤: 将正确的用户名和密码手动保存在本地的userinfo.txt文件中。 保存格式为: username=zhangsan&password=123 让用户键盘录入用户名和密码 比较用户录入的和正确的用户名密码是否一致 如果一致则打印登入成功 如果不一致就登入失败 */ // 1、读取正确的用户名和密码 BufferedReader br = new BufferedReader(new FileReader("lkb04-File&IOCode\\b.txt")); String line = br.readLine(); br.close(); System.out.println(line); String[] userInfo = line.split("&"); String[] arr1 = userInfo[0].split("="); String[] arr2 = userInfo[1].split("="); String[] arr3 = userInfo[2].split("="); String rightName = arr1[1]; String rightPwd = arr2[1]; //System.out.println(rightName); //System.out.println(rightPwd); // count: 登录失败的次数 int count = Integer.parseInt(arr3[1]); // 2、键盘录入用户名和密码 Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String name = sc.next(); System.out.println("请输入密码:"); String pwd = sc.next(); // 3、比较 if (rightName.equals(name) && rightPwd.equals(pwd)) { System.out.println("登入成功"); writeInfo("username="+rightName+"&password="+rightPwd+"&count="+count); }else { count++; if(count <= 3){ System.out.println("登入失败, 登录失败的次数为:" + count); } else{ System.out.println("登录失败次数过多, 已被锁定"); } writeInfo("username="+rightName+"&password="+rightPwd+"&count="+count); } } /* * 作用: * 写出一个字符串到本地文件中 * 参数: * 要写出的字符串 * */ public static void writeInfo(String info) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("lkb04-File&IOCode\\b.txt")); bw.write(info); bw.close(); } }