本文主要是介绍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新手练习:模拟五人游戏框架代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!