java新手练习:模拟五人游戏框架代码

编程入门 行业动态 更新时间:2024-10-25 08:21:35

java新手练习:模拟<a href=https://www.elefans.com/category/jswz/34/1699432.html style=五人游戏框架代码"/>

java新手练习:模拟五人游戏框架代码

文章目录

  • 主代码
  • 英雄类
  • 运行效果

游戏背景: 假设需要创建一个模拟五人组队游戏的框架

主代码

package www.heima;import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;import java.io.*;
import java.time.chrono.HijrahEra;
import java.util.ArrayList;
import java.util.Scanner;
/*
** @aothor cds* @create 2019/3/31 - 9:07
*/public class Game5Persion {private static ArrayList<hero> heroArrayList;public static void main(String[] args) throws IOException {//1.创建一个集合,用来存储五个英雄ArrayList<hero> HeroList = new ArrayList<>();//        2.读文件,把数据加载到内存中loadFriends(HeroList);//        3.判断内容是不是新的boolean isNew = HeroList.isEmpty();System.out.println("判断有没有好友:" + isNew);//        如果是新的,创建五个对象加入集合中if(isNew){//            System.out.println(" 加入五个新队友 ");addHeros(HeroList);}//        如果不是新的,无需创建,直接开始//        4.开始游戏System.out.println("游戏开始");
//                输出英雄阵容,信息showHeros(HeroList);System.out.println("showFightingCapacity(HeroList) = " + showFightingCapacity(HeroList));//        5.游戏结束System.out.println( "游戏结束");//        6.判断是不是原始阵容是不是新的
//                是新的if(isNew) {saveFriends(HeroList);System.out.println("新阵容,加好友");}else {
//                 不是新的System.out.println(" 已经是好友,无需加好友");}
//        7.退出游戏System.out.println("\"退出游戏\" = " + "退出游戏");}//添加新英雄private static void addHeros(ArrayList<hero> HeroList){Scanner sc = new Scanner(System.in);for (int i = 1 ; i < 6 ; i++ ){System.out.println("添加第" + i +"位新英雄:格式为String name, int attckt, String type: " );String line = sc.nextLine();String[] arr = line.split(",");String name = arr[0];int attckt = Integer.parseInt(arr[1]);String type = arr[2];
//            String name = sc.next();
//            int attckt = sc.nextInt();
//            String type = sc.next();HeroList.add(new hero(name,attckt,type));}System.out.println("组队成功 ");}//    展示英雄public static void showHeros(ArrayList<hero> Herolist){int i = 1;for (hero heros:Herolist) {System.out.println("heros = " + heros);}}//    计算总攻击力public static int showFightingCapacity(ArrayList<hero> Herolist) {int num = 0;for (hero heros : Herolist) {num += heros.getAttckt();}return num;}//    将好友信息写入文件public static void saveFriends(ArrayList<hero> heroesList) throws IOException {try (BufferedWriter bw = new BufferedWriter(new FileWriter("friends.txt"))) {for (hero heros : heroesList) {bw.write(heros.toString());bw.newLine();}}}//    定义方法,读取文件,数据加载到集合中static void loadFriends(ArrayList<hero> heroArrayList) throws IOException {
//        Game5Persion.heroArrayList = heroArrayList;try (BufferedReader br = new BufferedReader(new FileReader("friends.txt"))) {String line;while ((line = br.readLine()) != null){String[] arr = line.split(",");String name = arr[0];int atackt = Integer.parseInt(arr[1]);String type = arr[2];heroArrayList.add(new hero(name,atackt,type));}}}}

英雄类

package www.heima;/*** @aothor cds* @create 2019/3/31 - 9:08*/
public class hero {private String name;private int attckt;private String type;public hero() {}public hero(String name, int attckt, String type) {this.name = name;this.attckt = attckt;this.type = type;}public String getName() {return name;}public int getAttckt() {return attckt;}public String getType() {return type;}public void setName(String name) {this.name = name;}public void setAttckt(int attckt) {this.attckt = attckt;}public void setType(String type) {this.type = type;}@Overridepublic String toString() {return  "name = " + name + "," + attckt + ",type = " + type;
//                "name='" + name + '\'' +
//                ", attckt=" + attckt +
//                ", type='" + type ;}
}

运行效果

friends.txt中没有预录信息时:

friends.txt中有预录信息时:

更多推荐

java新手练习:模拟五人游戏框架代码

本文发布于:2024-03-23 21:00:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:五人   框架   新手   代码   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!