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
<think>根据限时模式的修改,我们需要在控制台界面中支持限时模式。主要修改点包括: 1. 在开始新游戏时,提供选择是否进入限时模式。 2. 如果选择限时模式,则让用户输入时间限制(秒)。 3. 在游戏过程中显示剩余时间(限时模式下)。 4. 在游戏循环中,如果时间耗尽,则游戏结束。 具体修改如下: 1. 修改`startGame`方法,让用户选择游戏模式(普通模式或限时模式)。 2. 如果选择限时模式,则创建`Game`对象时使用限时模式的构造函数。 3. 在`playGame`方法中,如果是限时模式,则在界面上显示剩余时间。 4. 修改游戏循环,在限时模式下,如果剩余时间<=0,则游戏结束。 注意:由于在`Game`类中已经实现了限时模式,我们只需要在界面中调用即可。 修改后的代码:</think>以下是针对限时模式修改后的控制台界面代码,整合了计时器显示和模式选择功能: ```java package game.game2048; import game.game2048.game.Direction; import game.game2048.game.Game; import java.sql.SQLException; import java.util.List; 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() { /* 原有代码不变 */ } private void login() throws SQLException { /* 原有代码不变 */ } private void register() throws SQLException { /* 原有代码不变 */ } public void captcha() { /* 原有代码不变 */ } public static String getCode() { /* 原有代码不变 */ } public static boolean validateCaptcha(String captcha, String 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.println("7. 退出游戏"); System.out.print("请选择: "); int choice = scanner.nextInt(); scanner.nextLine(); System.out.println(); switch (choice) { case 1: startGame(false); // 普通模式 break; case 2: startGame(true); // 限时模式 break; case 3: loadGame(); break; case 4: showRankings(); break; case 5: showUserHistory(); break; case 6: currentUserId = -1; System.out.println("已退出登录"); break; case 7: System.exit(0); default: System.out.println("无效选择!"); } } // 启动游戏(支持普通/限时模式) private void startGame(boolean isTimedMode) throws SQLException { int timeLimit = 0; if (isTimedMode) { System.out.print("请输入时间限制(): "); timeLimit = scanner.nextInt(); scanner.nextLine(); } while (true) { Game game = isTimedMode ? new Game(true, timeLimit) : // 限时模式 new Game(); // 普通模式 playGame(game, isTimedMode); System.out.println("请选择是否重新开始: 1 - 重新开始 其他 - 返回菜单"); int res = scanner.nextInt(); scanner.nextLine(); if (res != 1) { break; } } } // 游戏存档(保持不变) private void loadGame() throws SQLException { /* 原有代码不变 */ } // 游戏主循环(增加时间显示) private void playGame(Game game, boolean isTimedMode) 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); } // 限时模式显示剩余时间 if (isTimedMode) { System.out.print("\t剩余时间: " + game.getRemainingTime() + "秒"); } 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()); if (isTimedMode && game.getRemainingTime() <= 0) { System.out.println("时间到! 最终分数: " + game.getScore()); } else { System.out.println("游戏结束! 最终分数: " + game.getScore()); } gameDAO.saveScore(currentUserId, game.getScore()); } // 打印地图(保持不变) private void displayBoard(int[][] board) { /* 原有代码不变 */ } // 排行榜(保持不变) private void showRankings() throws SQLException { /* 原有代码不变 */ } // 游戏历史(保持不变) private void showUserHistory() throws SQLException { /* 原有代码不变 */ } } ``` ### 主要修改内容: 1. **主菜单重构**: - 将"开始新游戏"拆分为两个选项: ```java System.out.println("1. 开始新游戏(普通模式)"); System.out.println("2. 开始限时模式"); ``` 2. **游戏启动逻辑**: - 增加`startGame(boolean isTimedMode)`方法处理不同模式 - 限时模式下提示用户输入时间限制: ```java if (isTimedMode) { System.out.print("请输入时间限制(): "); timeLimit = scanner.nextInt(); } ``` 3. **游戏实例创建**: - 根据模式选择不同构造函数: ```java Game game = isTimedMode ? new Game(true, timeLimit) : // 限时模式 new Game(); // 普通模式 ``` 4. **游戏界面增强**: - 在游戏循环中显示剩余时间: ```java if (isTimedMode) { System.out.print("\t剩余时间: " + game.getRemainingTime() + "秒"); } ``` - 游戏结束区分时间耗尽和常规结束: ```java if (isTimedMode && game.getRemainingTime() <= 0) { System.out.println("时间到! 最终分数: " + game.getScore()); } else { System.out.println("游戏结束! 最终分数: " + game.getScore()); } ``` ### 使用示例: ``` === 主菜单 === 1. 开始新游戏(普通模式) 2. 开始限时模式 3. 加载存档 4. 查看排行榜 5. 查看历史记录 6. 退出登录 7. 退出游戏 请选择: 2 请输入时间限制(): 120 当前分数: 0 最高分: 256 剩余时间: 119秒 ▢ 2 ▢ 4 ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ 操作: w() s() a() d() q(退出) x(存档) z(撤回) 输入操作: ``` ### 功能特点: 1. **双模式无缝切换**:用户可自由选择普通模式或限时模式 2. **自定义时间限制**:支持设置任意时长(秒为单位) 3. **实时时间显示**:游戏界面清晰展示剩余时间 4. **智能结束判定**:区分时间耗尽和常规游戏结束 5. **存档兼容性**:存档系统保持不变,兼容两种模式 此实现保持了原有功能的完整性,同时为限时模式提供了直观的用户界面和流畅的游戏体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值