wordpress编辑可视化网站推广优化外包便宜
- 作者: 多梦笔记
- 时间: 2026年02月18日 17:19
当前位置: 首页 > news >正文
wordpress编辑可视化,网站推广优化外包便宜,中文网站设计,dedecms 网站还原教程Java 数字炸弹小游戏#xff08;控制台版#xff09; IO 数据存储 数字炸弹小游戏概述功能实现实体类User.java 玩家信息实体类GameRecode.java 游戏记录实体类 自定义异常AccountLockedException.java 账号锁定异常PasswordErrorException.java 密码错误异常UnknowAccountEx… Java 数字炸弹小游戏控制台版 IO 数据存储 数字炸弹小游戏概述功能实现实体类User.java 玩家信息实体类GameRecode.java 游戏记录实体类 自定义异常AccountLockedException.java 账号锁定异常PasswordErrorException.java 密码错误异常UnknowAccountException 账号不存在异常 游戏主类Game.java 游戏主类 数字炸弹小游戏概述 数字炸弹控制台版小游戏是Java 集合、流程控制、IO、异常、常用类等技术的综合练习。核心需求如下 实现数字炸弹游戏要求如下1、创建游戏菜单1注册2登录3开始游戏4游戏记录5游戏排行6退出游戏2、菜单含义1注册注册游戏玩家要求玩家名字不能重复2登录使用玩家名字和密码进行登陆3开始游戏进入游戏只有登录的玩家才可以开始游戏4游戏记录展示当前玩家的游戏记录序号、炸弹、猜测次数、游戏开始时间、游戏结束时间、积分5游戏排行展示所有用户的昵称、游戏次数、总积分倒序6退出游戏结束游戏3、游戏规则a、生成100以内的随机值作为炸弹b、从控制台输入猜测的数值c、每次输入猜测值之后缩小猜测范围直到猜中为止d、本轮游戏结束之后反馈菜单继续游戏、返回菜单e、本轮游戏结束之后根据猜测次数和游戏时间换算积分4、游戏积分a、1-3次内猜中含3次时间在20秒以内积分10b、4-6次内猜中含6次时间在21-60秒积分5c、7-10次内猜中含10次时间在60秒以上积分2d、10次以上猜中不得分5、拓展功能a、给游戏排行榜新加菜单选项支持升序、降序的展示菜单。b、新增玩家管理功能锁定玩家和解锁玩家。c、给登录功能增加异常处理自定义账号不存在异常、认证失败异常、锁定异常d、基于 IO 存储玩家信息和记录信息到文件。功能实现 实体类 User.java 玩家信息实体类 package com.riu.collect.game2.entity;import java.io.Serializable; import java.util.Objects;/*** 玩家类玩家的相关信息/ public class User implements ComparableUser, Serializable {// 账号private String name;// 密码private String password;// 总的猜测数量、游戏的次数private Integer totalCount;// 总的积分private Integer totalPoints;// 玩家锁定、解锁的标记false解锁状态true锁定状态private boolean isLock false;public User() {}public User(String name, String password, Integer totalCount, Integer totalPoints) {this.name name;this.password password;this.totalCount totalCount;this.totalPoints totalPoints;}public User(String name, String password, Integer totalCount, Integer totalPoints, boolean isLock) {this.name name;this.password password;this.totalCount totalCount;this.totalPoints totalPoints;this.isLock isLock;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Integer getTotalCount() {return totalCount;}public void setTotalCount(Integer totalCount) {this.totalCount totalCount;}public Integer getTotalPoints() {return totalPoints;}public void setTotalPoints(Integer totalPoints) {this.totalPoints totalPoints;}public boolean isLock() {return isLock;}public void setLock(boolean lock) {isLock lock;}Overridepublic String toString() {return User{ name name \ , password password \ , totalCount totalCount , totalPoints totalPoints , isLock isLock };}/** equals 和 hashCode 可以帮助我们判断对象的唯一性* 当前类的唯一性的条件是用户名字玩家在注册时候可以保证唯一性* param o* return/Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;User user (User) o;return Objects.equals(name, user.name);}Overridepublic int hashCode() {return Objects.hash(name);}Overridepublic int compareTo(User o) {return this.getName().compareTo(o.name);} }GameRecode.java 游戏记录实体类 package com.riu.collect.game2.entity;import java.io.Serializable; import java.util.Date;/** 每次游戏的记录信息/ public class GameRecode implements Serializable {// 炸弹private Integer boom;// 猜测次数private Integer count;// 游戏开始时间private Date startTime;// 游戏结束时间private Date endTime;// 积分private Integer points;public Integer getBoom() {return boom;}public void setBoom(Integer boom) {this.boom boom;}public Integer getCount() {return count;}public void setCount(Integer count) {this.count count;}public Date getStartTime() {return startTime;}public void setStartTime(Date startTime) {this.startTime startTime;}public Date getEndTime() {return endTime;}public void setEndTime(Date endTime) {this.endTime endTime;}public Integer getPoints() {return points;}public void setPoints(Integer points) {this.points points;}Overridepublic String toString() {return GameRecode{ boom boom , count count , startTime startTime , endTime endTime , points points };} }自定义异常 AccountLockedException.java 账号锁定异常 package com.riu.collect.game2.exception;/** 玩家账号锁定异常/ public class AccountLockedException extends Exception {public AccountLockedException() {super();}public AccountLockedException(String message) {super(message);}Overridepublic String getMessage() {return super.getMessage();} }PasswordErrorException.java 密码错误异常 package com.riu.collect.game2.exception;/** 玩家密码错误认证失败/ public class PasswordErrorException extends Exception {public PasswordErrorException() {super();}public PasswordErrorException(String message) {super(message);}Overridepublic String getMessage() {return super.getMessage();} }UnknowAccountException 账号不存在异常 package com.riu.collect.game2.exception;/** 玩家账号不存在异常/ public class UnknowAccountException extends Exception {public UnknowAccountException() {super();}public UnknowAccountException(String message) {super(message);}Overridepublic String getMessage() {return super.getMessage();} }游戏主类 Game.java 游戏主类 package com.riu.collect.game2;import com.riu.collect.game2.entity.GameRecode; import com.riu.collect.game2.entity.User; import com.riu.collect.game2.exception.AccountLockedException; import com.riu.collect.game2.exception.PasswordErrorException; import com.riu.collect.game2.exception.UnknowAccountException;import java.io.; import java.text.SimpleDateFormat; import java.util.; import java.util.stream.Stream;public class Game {// 创建一个用户的集合private ListUser userList new ArrayList();// 玩家和记录的对应关系private MapString, ListGameRecode recodeMap new HashMap();// 当前登录的玩家名字private String loginName;// 判断玩家是登录的标记默认 false 表示没有登录。// 等成功之后可以进行一些菜单操作boolean isLogin false;// 两个文件路径private String userInfoPath src/com/riu/collect/game2/userInfo.txt;private String recodeInfoPath src/com/riu/collect/game2/recodeInfo.txt;// 定义公共的输入对象private Scanner scanner new Scanner(System.in);public static void main(String[] args) {Game game new Game();game.init();while (true){game.menu();}}/** 初始化系统从文件中将数据读取到 List/private void init(){try {/ 用户玩家信息读取 /File userInfoFile new File(userInfoPath);if(userInfoFile.exists()){ObjectInputStream oisUserInfoStream new ObjectInputStream(new FileInputStream(userInfoPath));Object userInfoObject oisUserInfoStream.readObject();if(userInfoObject ! null){userList (ListUser) userInfoObject;}oisUserInfoStream.close();}/ 用户玩家游戏记录读取 /File recodeInfoFile new File(recodeInfoPath);if(recodeInfoFile.exists()) {ObjectInputStream oisRecodeInfoStream new ObjectInputStream(new FileInputStream(recodeInfoPath));Object recodeObject oisRecodeInfoStream.readObject();if (recodeObject ! null) {recodeMap (MapString, ListGameRecode) recodeObject;}oisRecodeInfoStream.close();}} catch (Exception e) {throw new RuntimeException(e);}}public void menu(){System.out.println();System.out.println(\t\t\t1注册);System.out.println(\t\t\t2登录);if(isLogin){System.out.println(\t\t\t3开始游戏);System.out.println(\t\t\t4游戏记录);}System.out.println(\t\t\t5游戏排行);System.out.println(\t\t\t6退出游戏);System.out.println(\t\t\t7玩家管理);System.out.println();System.out.print(请输入菜单编号);int menuNum scanner.nextInt();switch (menuNum) {case 1:// 注册reg();break;case 2:// 登录try {login();} catch (UnknowAccountException | PasswordErrorException | AccountLockedException e) {System.out.println(e.getMessage());}break;case 3:// 开始游戏startGame();break;case 4:// 展示游戏记录gameRecode();break;case 5:// 展示游戏排行榜gameTop();break;case 6:// 退出游戏exit();break;case 7:// 退出游戏userControl();break;}}/** 注册/private void reg() {System.out.print(请输入玩家昵称);String name scanner.next();System.out.print(请输入密码);String password scanner.next();// 把输入的信息封装到对象中User user new User();user.setName(name);user.setPassword(password);// 判断集合中是否已经存在玩家信息// 这里使用 contains 对比要求 user 类必须重写 equals 和 hashcode 方法if(userList.contains(user)){System.out.println(玩家已经存在快去开始游戏吧…);} else {// 把对象添加到集合userList.add(user);System.out.println(注册成功快去开始游戏吧…);}}/** 登录* 1、账号是否存在* 2、账号密码是否匹配* 3、账号是否被锁定** 账号是用户输入的根据用户输入的信息递进验证账号是否可以使用。* 1、账号是否存在拿着用户输入的信息从集合中获取用户的信息对象。可以把用户对象临时存储一下。* 2、账号密码是否匹配拿着用户输入的密码和获取到的用户对象的密码对比。* 3、账号是否被锁定根据获取到的用户的对象信息中的锁定状态判断。/private void login() throws UnknowAccountException, PasswordErrorException, AccountLockedException {System.out.print(请输入玩家昵称);String name scanner.next();System.out.print(请输入密码);String password scanner.next();// 用于临时存储用户信息的对象User tempUser null;// 循环遍历找账号信息for (User item : userList) {String tempName item.getName();// 用户输入的账号和集合中获取的用户的账号对比// 找到用户了。if (name.equals(tempName)) {tempUser item;break;}}// 基于账号信息tempUser做3种别判断// 1、账号是否存在if(tempUser null){// System.out.println(账号不存在);throw new UnknowAccountException(该玩家不存在);} else {// System.out.println(账号存在);// 2、账号密码是否匹配// 玩家输入的密码和 tempUser 的密码匹配if(password.equals(tempUser.getPassword())){// System.out.println(密码正确);// 3、账号是否被锁定if(tempUser.isLock()){// System.out.println(账号被锁定);throw new AccountLockedException(账号被锁定);} else {System.out.println(账号可用登录成功);// 玩家存在的标记修改为 trueisLogin true;// 登录成功之后把当前玩家的名字赋值给全局的变量loginName name;// 登录成功了给当前用户创建一个用于存储游戏记录的list结果recodeMap.put(name, new ArrayList());}} else {// System.out.println(密码不正确认证失败);throw new PasswordErrorException(密码错误);}}}/** 开始游戏/private void startGame() {// 可以给循环做一个标记lab 就是这个循环的标记名字名字可以任意。lab:while (true){/ 游戏本身需要的相关变量 /// 区间开始和结束int start 0;int end 100;/ 游戏本身需要的相关变量 // 游戏记录数据需要的相关变量 /// 每一轮游戏的过程// 随机炸弹int boom new Random().nextInt(100);// 开始和结束时间Date startTime new Date();Date endTime null;// 每一轮游戏猜的次数int count 0;// 每一轮游戏的积分变量int points 0;/ 游戏记录数据需要的相关变量 /while (true){// 猜测次数的累加count;System.out.print(请猜);int num scanner.nextInt();if(num boom){end num;} else if(num boom){start num;} else {System.out.println(游戏结束);/ 游戏结束才开始收集游戏信息开始 /// 结束时间endTime new Date();// 计算时间间隔long l (endTime.getTime() - startTime.getTime()) / 1000;// 计算次数if (count 1 count 3) {points 10;} else if (count 4 count 6) {points 5;} else if (count 7 count 10) {points 2;} else {points 0;}// 创建记录对象封装游戏过程中的记录信息GameRecode gameRecode new GameRecode();gameRecode.setBoom(boom);gameRecode.setCount(count);gameRecode.setStartTime(startTime);gameRecode.setEndTime(endTime);gameRecode.setPoints(points);// 这些记录是哪个玩家的。把记录和玩家挂钩。ListGameRecode gameRecodeList recodeMap.get(loginName);gameRecodeList.add(gameRecode);/ 游戏结束才开始收集游戏信息结束 /// 跳出猜测的循环break;}System.out.println(游戏继续区间是[ start , end ]);}System.out.println(*************************************);System.out.println(\t\t\t1继续游戏);System.out.println(\t\t\t2返回菜单);System.out.println(***********************************);System.out.print(请输入菜单编号);String menuNum scanner.next();switch (menuNum) {case 1:// 结束的是 switchbreak;case 2:// 结束标记是 lab 的循环break lab;}}}/ 展示当前用户的游戏记录/private void gameRecode() {// 通过玩家和记录的Map集合获取登录玩家的记录集合ListGameRecode gameRecodeList recodeMap.get(loginName);// 遍历当前玩家的记录集合System.out.println(序号\t炸弹\t次数\t开始时间\t\t\t\t结束时间\t\t\t\t积分);int i 1;for (GameRecode gameRecode : gameRecodeList) {System.out.println(i \t gameRecode.getBoom() \t gameRecode.getCount() \t formatTime(gameRecode.getStartTime()) \t formatTime(gameRecode.getEndTime()) \t gameRecode.getPoints() \t);}}/** 格式化时间的方法* param date* return/private String formatTime(Date date) {SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);return sdf.format(date);}/** 按照积分的降序展示每个用户的信息* 用户的昵称、游戏次数、总积分倒序*/private void gameTop() {// 遍历用户结果集汇总玩家的记录数据循环走完之后userList 中的每个玩家的游戏次数和总积分都会有值了for (User user : userList) {// 获取当前用户游戏记录的结果集ListGameRecode gameRecodeList recodeMap.get(user.getName());// 遍历游戏记录结果集统计所有积分的和int sumPoints 0;for (GameRecode gameRecode : gameRecodeList) {Integer points gameRecode.getPoints();sumPoints points;}// 将统计好的结果在赋值给用户对象user.setTotalCount(gameRecodeList.size());user.setTotalPoints(sumPoints);}System.out.println(———————————–);System.out.println(\t\t\t1升序展示);System.out.println(\t\t\t2降序展示);System.out.println(———————————–);System.out.print(请输入菜单编号);String menuNum scanner.next();System.out.println(昵称\t游戏次数\t总积分);// 默认升序排序调用 sorted 之后返回一个可以继续操作的流StreamUser newStream userList.stream();switch (menuNum) {case 1:newStream newStream.sorted(Comparator.comparing(User::getTotalPoints));break;case 2:newStream newStream.sorted(Comparator.comparing(User::getTotalPoints).reversed());break;}// 输出放到最后newStream.forEach(user - {System.out.println(user.getName() \t user.getTotalCount() \t\t user.getTotalPoints());});// 根据积分倒叙排序// 1、List 转化为 Stream 流对象// 2、调用 Stream 的 sorted 方法进行排序// 3、sorted 需要传递一个排序的规则这个规则是 Comparator 类型。这里思考如何获取 Comparator 类型// 4、Comparator.comparing 方法可以返回一个 Comparator 类型也就是排序的规则对象。其中还要知道排序的数据是哪个是积分// 解析Comparator.comparing 构建一个规则对象。方法传递要排序的关键字数据属性。reversed() 就是降序/userList.stream().sorted(Comparator.comparing(User::getTotalPoints).reversed()).forEach(user - {System.out.println(user.getName() \t user.getTotalCount() \t\t user.getTotalPoints());});/}/*** 结束游戏/private void exit() {// 添加 IO 的操作把玩家的信息写入到文件。try {/ 用户信息的存储 /// 也就是把 userList 的数据写入到文件File userInfoFile new File(userInfoPath);if(!userInfoFile.exists()){userInfoFile.createNewFile();}// 创建对象的输出流ObjectOutputStream oosUserInfoSteam new ObjectOutputStream(new FileOutputStream(userInfoFile));oosUserInfoSteam.writeObject(userList);oosUserInfoSteam.close();/ 用户游戏记录的存储 /// 也就是把 userList 的数据写入到文件File recodeFile new File(recodeInfoPath);if(!recodeFile.exists()){recodeFile.createNewFile();}// 创建对象的输出流ObjectOutputStream oosRecodeInfoSteam new ObjectOutputStream(new FileOutputStream(recodeInfoPath));oosRecodeInfoSteam.writeObject(recodeMap);oosRecodeInfoSteam.close();} catch (Exception e) {e.printStackTrace();System.out.println(文件操作异常!);}System.exit(0);}/** 玩家管理对玩家进行锁定和解锁*/private void userControl(){System.out.print(请输入要管理的玩家昵称);// 要匹配的玩家名字String userName scanner.next();User lockUser null;boolean isLock false;for (User user : userList) {String name user.getName();if(userName.equals(name)){lockUser user;break;}}if(lockUser ! null){// 找到玩家之后再进行业务处理System.out.println(^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^);System.out.println(\t\t\t1锁定玩家);System.out.println(\t\t\t2解锁玩家);System.out.println(^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^);System.out.print(请输入菜单编号);String menuNum scanner.next();switch (menuNum){case 1:isLock true;break;case 2:isLock false;break;}lockUser.setLock(isLock);System.out.println(^^^^^操作成功^^^^^);} else {System.out.println(该玩家不存在);}} }
相关文章
-
wordpress本站运行wordpress好用的模板
wordpress本站运行wordpress好用的模板
- 站长
- 2026年02月18日
-
wordpress本地怎么迁移到服务器廊坊短视频优化公司
wordpress本地怎么迁移到服务器廊坊短视频优化公司
- 站长
- 2026年02月18日
-
wordpress本地建站网站建设服务包含内容
wordpress本地建站网站建设服务包含内容
- 站长
- 2026年02月18日
-
wordpress博客福利网整站源码h5响应式网站做动画
wordpress博客福利网整站源码h5响应式网站做动画
- 站长
- 2026年02月18日
-
wordpress博客统计湖北seo服务
wordpress博客统计湖北seo服务
- 站长
- 2026年02月18日
-
wordpress博客源码默认密码安卓优化大师老版本
wordpress博客源码默认密码安卓优化大师老版本
- 站长
- 2026年02月18日
