简化System.out.print

为简化Java程序中的打印操作,本文介绍了一个自定义的打印工具类。该工具类提供了三种打印方法,分别是println(Object obj)、println()和print(Object obj),通过静态导入方式使用,旨在提高代码的可读性和简洁性。
我是一个懒人,喜欢简单的方法,System.out.print着实打烦了于是就写了一个工具类取代之
工具类代码如下:
package com.liusy.util;
import java.io.*;

public class Print {
  
  public static void println(Object obj) {
    System.out.println(obj);
  }
  // 换行输出
  public static void println() {
    System.out.println();
  }
  // 不换行输出,
  public static void print(Object obj) {
    System.out.print(obj);
  }
} 
实现代码:
import static com.liusy.util.Print.*;
public class SoEasy {
	public static void main(String[] args) {
		print("输出,so easy");
	}
}

这里要注意,import static com.liusy.util.Print.*; ,这里有一个JAVASE5出现的概念,静态引入
虽然我们也可以在程序中直接import static java.lang.System.out ;也有相同的作用,
但是因为静态导入使得类方法和类变量的定义位置变得模糊,所以加大人员阅读、理解代码的难度
所以在这里我们引入自己的工具类
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 static final int FIXED_TIME_LIMIT = 60; 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 { System.out.println("=== 选择游戏模式 ==="); System.out.println("1. 普通模式"); System.out.println("2. 限时模式"); System.out.println("3. 返回主菜单"); System.out.print("请选择: "); int modeChoice = scanner.nextInt(); scanner.nextLine(); if (modeChoice == 3) return; // 返回主菜单 boolean isTimedMode = (modeChoice == 2); int timeLimit = isTimedMode ? FIXED_TIME_LIMIT : 0; while (true) { Game game = isTimedMode ? new Game(true, timeLimit) : // 限时模式 new Game(); // 普通模式 playGame(game, isTimedMode); System.out.println("请选择是否重新开始: 1 - 重新开始 2 - 返回菜单"); int res = scanner.nextInt(); scanner.nextLine(); if (res != 1) { break; } } } //游戏存档 private void loadGame() throws SQLException { List<GameDAO.GameSave> saves = gameDAO.getUserSaves(currentUserId); if (saves.isEmpty()) { System.out.println("没有找到存档!"); return; } System.out.println("=== 您的存档 ==="); System.out.printf("%-6s %-20s %-8s %-10s %-15s%n", "ID", "存档名称", "分数", "模式", "创建时间"); for (GameDAO.GameSave save : saves) { String modeInfo = save.isTimedMode() ? "限时模式" : "普通模式"; System.out.printf("%-6d %-20s %-8d %-10s %-15s%n", save.getId(), save.getSaveName(), save.getScore(), modeInfo, save.getTimestamp()); } System.out.print("输入存档ID: "); int saveId = scanner.nextInt(); scanner.nextLine(); GameDAO.GameSave gameSave = gameDAO.loadGame(saveId); if (gameSave != null) { // 创建游戏实例时传递模式信息 Game game = new Game(gameSave.getBoardState(), gameSave.getScore(), gameSave.isTimedMode(), gameSave.getTimeLimit(), gameSave.getRemainingTime()); // 传递模式信息给游戏循环 playGame(game, gameSave.isTimedMode()); } else { System.out.println("加载存档失败!"); } } //游戏规则 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) { 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("\t0"); } 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(false); 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); } } } }代码拆分类,不做修改
最新发布
11-18
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值