EBU4201(2021/22) Lab5 Q2

Lab5 Q2代码基础建立在Q1所写等等程序上:

EBU4201(2021/22) Lab5 Q1icon-default.png?t=M4ADhttp://t.csdn.cn/baQ14代码1:Monster.java

/**
 * @author andtrees
 * @version 1.0
 */
public abstract class Monster {
    String name;
    double spAttackProbability = 0.2;

    public Monster(String name){
        this.name = name;
    }
    public Monster(String name, double spAttackProbability) {
        this.name = name;
        this.spAttackProbability = spAttackProbability;
    }
    public abstract int specialAttack();

    public final int attack() {
        int damage;
        //随机数小于sp的时候用specialAttack,否则用general Attack(常规攻击
        double probability = Math.random();
        if(probability < spAttackProbability){
            damage = specialAttack();
        }
        else {
            damage = (int) (Math.random() * 5 + 1);
            System.out.println(this.name + ", of type "
                    + getClass() + ", attacks generically: "
                    + damage + " points damage caused. ");
        }
        return damage;
    }

    public void move(int direction) {
        switch(direction) {
            case 1: {
                System.out.println(this.name +" is moving 1 step North. ");
                break;
            }
            case 2:{
                System.out.println(this.name +" is moving 1 step East. ");
                break;
            }
            case 3:{
                System.out.println(this.name +" is moving 1 step South. ");
                break;
            }
            default:
                System.out.println(this.name +" is moving 1 step West. ");
                break;
        }
    }
}

代码2:Dragon.java

/**
 * @author andtrees
 * @version 1.0
 */
//public abstract class Dragon extends Monster{
public class Dragon extends Monster{
    public Dragon(String name){
        super(name);
        this.name = name;
    }
    public Dragon(String name, double spAttackProbability){
        super(name,spAttackProbability);
        this.name = name;
        this.spAttackProbability = spAttackProbability;
    }
    public int specialAttack(){
        int x = (int)(Math.random()*50 + 1);
        System.out.println(this.name + ", of type "
                + getClass() + ", attacks generically: "
                + x + " points damage caused. ");
        return x;
    }

}

代码3:Troll.java

/**
 * @author andtrees
 * @version 1.0
 */
public class Troll extends Monster{
    public Troll(String name){
        //super(name);
        //this.name = name;
        super(name);
        if(name.equals("Saul")||name.equals("Salomon")){
            System.out.println("!!ERROR!! This name is not available!!!");
            this.name = "Detritus";
        }else{
            this.name = name;
        }
    }
    public Troll(String name, double spAttackProbability){
        super(name,spAttackProbability);
        //this.name = name;
        if(name.equals("Saul")||name.equals("Salomon")){
            System.out.println("!!ERROR!! This name is not available!!!");
            this.name = "Detritus";
        }else{
            this.name = name;
        }
        this.spAttackProbability = spAttackProbability;
    }
    public int specialAttack(){
        int x = (int)(Math.random()*15 + 1);
        System.out.println(this.name + ", of type "
                + getClass() + ", attacks by hitting with a stick "
                + x + " points damage caused. ");
        return x;
    }
}

代码4:TestingMonster.java


import java.util.ArrayList;

public class TestingMonster {
    public static void main(String[] args){
        ArrayList<Monster> monsters = new ArrayList<Monster>();
        monsters.add(new Dragon("Dragon1"));
        monsters.add(new Dragon("Dragon2"));

        monsters.add(new Troll("Troll1"));
        monsters.add(new Troll("Saul",20));
        int damageDone = 0;
        while (damageDone < 100) {
            for (Monster m : monsters) {
                m.move((int)(Math.random()*4) + 1);
                damageDone = damageDone + m.attack();
            }
        }
    }
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码有几个问题需要优化: 1. 如果其中一个文件读取失败,整个方法就会返回并且数据无法正确导入,应该在每个文件读取失败后继续读取其他文件。 2. 在读取文件时,最好使用绝对路径,因为相对路径可能会因为不同的执行环境而不同。 3. 在解析文件内容时,最好检查文件内容的行数是否相等,否则会导致数据不匹配。 以下是优化后的代码: ``` public void importData() { List<String> files = Arrays.asList("courses.txt", "scores.txt", "credits.txt", "semesters.txt", "types.txt"); List<List<String>> fileContents = new ArrayList<>(); for (String file : files) { try (Stream<String> stream = Files.lines(Paths.get("F:/ProgramData/IdeaProjects/BUPT-EBU6304G49-main/SE0514/src/data/" + file), StandardCharsets.UTF_8)) { fileContents.add(stream.collect(Collectors.toList())); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Failed to import data from file: " + file); } } if (fileContents.size() != 5) { JOptionPane.showMessageDialog(null, "Failed to import data"); return; } // Check if each file has the same number of lines int numLines = fileContents.get(0).size(); for (List<String> content : fileContents) { if (content.size() != numLines) { JOptionPane.showMessageDialog(null, "Failed to import data: files do not have the same number of lines"); return; } } // Clear current data this.courses.clear(); // Read data from files and add to list for (int i = 0; i < numLines; i++) { String course = fileContents.get(0).get(i); String score = fileContents.get(1).get(i); String credit = fileContents.get(2).get(i); String semester = fileContents.get(3).get(i); String courseType = fileContents.get(4).get(i); addData(course, credit, score, semester, courseType); this.courses.add(new Course(course, score, credit, semester, courseType)); } JOptionPane.showMessageDialog(null, "Data has been imported"); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值