System.out.println("哈哈".equals(username)) 返回false

本文讨论了在处理HTTP请求参数时遇到的问题,特别是当参数值包含特殊字符如' '时,如何正确解析并判断是否为特定字符串。文章提供了检查编码问题的方法,帮助开发者解决页面输入中出现的非预期字符问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String username = request.getParameter("username");
System.out.println(username);
System.out.println("哈哈".equals(username));//如果上一句打印出来是哈哈,本行代码运行结果是false,那么就是本页面的编码问题,是  哈哈的 编码问题
package game.game2048; /** * @BelongsProject: TimeGame * @BelongsPackage: Demo.game * @Author: 土里埋埋埋埋埋 * @CreateTime: 2025-07-31 11:51 * @Description: 游戏界面 * @Version: 1.0 */ import game.game2048.game.Direction; import game.game2048.game.Game; import java.sql.SQLException; import java.util.List; import java.util.Random; import java.util.Scanner; public class ConsoleUI { private Scanner scanner; private GameDAO gameDAO; private int currentUserId; public ConsoleUI() throws SQLException { scanner = new Scanner(System.in); gameDAO = new GameDAO(); currentUserId = -1; } public void start() throws SQLException { while (true) { if (currentUserId == -1) { showLoginMenu(); } else { showMainMenu(); } } } //获取登录菜单 private void showLoginMenu() { System.out.println("=== 2048 游戏 ==="); System.out.println("1. 登录"); System.out.println("2. 注册"); System.out.println("3. 退出"); System.out.print("请选择: "); int choice = scanner.nextInt(); scanner.nextLine(); try { switch (choice) { case 1: login(); break; case 2: register(); break; case 3: System.exit(0); default: System.out.println("无效选择!"); } } catch (SQLException e) { System.out.println("数据库错误: " + e.getMessage()); } } //登录界面 private void login() throws SQLException { System.out.print("用户名: "); String username = scanner.nextLine(); System.out.print("密码: "); String password = scanner.nextLine(); int userId = gameDAO.authenticateUser(username, password); if (userId != -1) { currentUserId = userId; captcha(); System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } } //注册界面 private void register() throws SQLException { System.out.print("用户名: "); String username = scanner.nextLine(); System.out.print("密码: "); String password = scanner.nextLine(); if (gameDAO.registerUser(username, password)) { System.out.println("注册成功!"); } else { System.out.println("注册失败,用户名可能已存在!"); } } //登录验证 public void captcha() { while (true) { Scanner sc = new Scanner(System.in); String code = getCode(); System.out.println("验证码:" + code); System.out.print("请输入验证码:"); String userInput = sc.next(); if (validateCaptcha(code, userInput)) { System.out.println("验证码正确!"); break; } else { System.out.println("验证码错误!"); } } } public static String getCode() { String box = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuilder captcha = new StringBuilder(); String codeBox = ""; for (int i = 0; i < 4; i++) { int index = random.nextInt(box.length()); char c = box.charAt(index); codeBox += c; } return codeBox; } public static boolean validateCaptcha(String captcha, String userInput) { return captcha.equalsIgnoreCase(userInput); } //游戏主菜单 private void showMainMenu() throws SQLException { System.out.println("=== 主菜单 ==="); System.out.println("1. 开始新游戏"); System.out.println("2. 加载存档"); System.out.println("3. 查看排行榜"); System.out.println("4. 查看历史记录"); System.out.println("5. 退出登录"); System.out.println("6. 退出游戏"); System.out.print("请选择: "); int choice = scanner.nextInt(); scanner.nextLine(); System.out.println(); switch (choice) { case 1: startGame(); break; case 2: loadGame(); break; case 3: showRankings(); break; case 4: showUserHistory(); break; case 5: currentUserId = -1; System.out.println("已退出登录"); break; case 6: System.exit(0); default: System.out.println("无效选择!"); } } //重新开始游戏 private void startGame() throws SQLException { while (true) { Game Game = new Game(); playGame(Game); System.out.println("请选择是否重新开始: 1 - 重新开始 其他 - 返回菜单"); int res = scanner.nextInt(); if (res == 1) { Game.resetGame(); } else { showMainMenu(); break; } } } //游戏存档 private void loadGame() throws SQLException { List<String> saves = gameDAO.getUserSaves(currentUserId); if (saves.isEmpty()) { System.out.println("没有找到存档!"); return; } System.out.println("您的存档:"); for (String save : saves) { System.out.println(save); } System.out.print("输入存档ID: "); int saveId = scanner.nextInt(); scanner.nextLine(); GameDAO.GameSave gameSave = gameDAO.loadGame(saveId); if (gameSave != null) { Game game = new Game(gameSave.boardState, gameSave.score); playGame(game); } else { System.out.println("加载存档失败!"); } } //游戏规则 private void playGame(Game game) throws SQLException { List<String> userMaxScore = gameDAO.maxscore(currentUserId); while (!game.isGameOver()) { System.out.print("当前分数: " + game.getScore()); System.out.print("\t"); for (String rank : userMaxScore) { System.out.print(rank); } System.out.println(); displayBoard(game.getBoard()); System.out.println("操作: w() s() a() d() q(退出) x(存档) z(撤回)"); System.out.print("输入操作: "); String input = scanner.nextLine().toLowerCase(); if (input.equals("q")) { return; } else if (input.equals("x")) { System.out.print("输入存档名称: "); String saveName = scanner.nextLine(); gameDAO.saveGame(currentUserId, saveName, game.getBoardState(), game.getScore()); System.out.println("游戏已保存!"); return; } else if (input.equals("z")) { if (game.canUndo()) { game.undo(); System.out.println("已撤回一步"); } else { System.out.println("无法撤回,一次仅可撤回一步"); } } else { Direction direction = null; switch (input) { case "w": direction = Direction.UP; break; case "s": direction = Direction.DOWN; break; case "a": direction = Direction.LEFT; break; case "d": direction = Direction.RIGHT; break; default: System.out.println("操作无效"); continue; } game.move(direction); } } displayBoard(game.getBoard()); System.out.println("游戏结束! 最终分数: " + game.getScore()); gameDAO.saveScore(currentUserId, game.getScore()); } //打印地图 private void displayBoard(int[][] board) { for (int i = 0; i < 4; i++) { System.out.print(""); for (int j = 0; j < 4; j++) { if (board[i][j] == 0) { System.out.print("\t▢"); } else { System.out.print("\t"); System.out.print(board[i][j]); } } System.out.println(); } } //排行榜 private void showRankings() throws SQLException { System.out.println("=== 排行榜 ==="); List<String> globalRanking = gameDAO.getGlobalRanking(); for (String rank : globalRanking) { System.out.println(rank); } } //游戏历史 private void showUserHistory() throws SQLException { System.out.println("=== 游戏历史 ==="); List<String> userHistory = gameDAO.getUserHistory(currentUserId); if (userHistory.isEmpty()) { System.out.println("暂无记录"); } else { for (String history : userHistory) { System.out.println(history); } } } }根据上述问题修改此界面代码
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值