Java练习(四)

一、文字版格斗游戏
人物属性:名字,血量,性别,长相
需求:双方进行攻击,直到一方血量为0倒下为止

代码思路:
1.角色系统:
  创建Role类封装角色属性:名字、血量、性别、长相
  长相根据性别从预设数组中随机选择(男性7种/女性7种)
2.战斗系统:
  设计攻击动作库(6种武侠招式模板)
  设计受伤描述库(8级渐进伤害反馈)
3.攻击流程:
   随机选择攻击描述
  生成1-20点随机伤害
  更新受击方血量(不低于0)
  根据剩余血量匹配受伤描述
4.游戏流程:
  角色A攻击角色B
  检测角色B是否死亡
  角色B攻击角色A
  检测角色A是否死亡
  当任一角色血量为0时结束战斗
  显示KO胜利信息

JavaBean类

import java.util.Random;

public class Role {
    private String name;
    private int blood;
    private char gender;
    private String face; //长相是随机的

    String[] boyfaces= {"风流俊雅","气宇轩昂","相貌英俊","五官端正","相貌平平","一塌糊涂","面目狰狞"};
    String[] girlfaces ={"美奂绝伦","沉鱼落雁","婷婷玉立","身材娇好","相貌平平","相貌简陋","惨不忍睹"};

    //attack 攻击描述:
    String[] attacks_desc={
            "%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。",
            "%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。",
            "%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。",
            "%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。",
            "%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。",
            "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。"
    };

    //injured 受伤描述:
    String[] injureds_desc={
            "结果%s退了半步,毫发无损",
            "结果给%s造成一处瘀伤",
            "结果一击命中,%s痛得弯下腰",
            "结果%s痛苦地闷哼了一声,显然受了点内伤",
            "结果%s摇摇晃晃,一跤摔倒在地",
            "结果%s脸色一下变得惨白,连退了好几步",
            "结果『轰』的一声,%s口中鲜血狂喷而出",
            "结果%s一声惨叫,像滩软泥般塌了下去"
    };

    public Role(){}

    public Role(String name, int blood,char gender) {
        this.name = name;
        this.blood = blood;
        this.gender = gender;
        //随机长相
        setFace(gender);
    }

    public String getFace() {
        return face;
    }

    public void setFace(char gender) {

        Random r = new Random();
        //长相是随机的
        if(gender == '男'){
            //从boyfaces里面随机长相
            int index = r.nextInt(boyfaces.length);
            this.face = boyfaces[index];
        }else if(gender == '女'){
            //从girlfaces里面随机长相
            int index = r.nextInt(boyfaces.length);
            this.face = girlfaces[index];
        }else {
            this.face = face;
        }
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    //定义一个方法用于攻击别人
    //方法的调用者去攻击参数

    public void attack(Role role) {
        Random r = new Random();
        int index = r.nextInt(attacks_desc.length);
        String kungfu = attacks_desc[index];
        //输出一个攻击效果
        System.out.printf(kungfu, this.getName(), role.getName());

        //计算造成的伤害1~20
        int hurt = r.nextInt(20) + 1;

        //剩余血量
        int remainBlood = role.getBlood() - hurt;
        //对剩余血量做一个验证,如果为负数了,就修改为0
        remainBlood = remainBlood < 0 ? 0 : remainBlood;

        //修改挨揍人的血量
        role.setBlood(remainBlood);
        System.out.printf("给其造成了%d点伤害",hurt);
        System.out.println();
        //this表示方法的调用者
        //受伤的描述
        //剩余血量大于90 0索引的描述
        //80~90 1索引的描述
        //70~80 2索引的描述
        //60~70 3索引的描述
        //40~60 4索引的描述
        //20~40 5索引的描述
        //10~20 6索引的描述
        //<10 7索引的描述
        if(remainBlood > 90){
            System.out.printf(injureds_desc[0],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        }else if (remainBlood > 80){
            System.out.printf(injureds_desc[1],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        } else if (remainBlood > 70) {
            System.out.printf(injureds_desc[2],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        } else if (remainBlood > 60) {
            System.out.printf(injureds_desc[3],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        }else if (remainBlood > 40) {
            System.out.printf(injureds_desc[4],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        } else if (remainBlood > 20) {
            System.out.printf(injureds_desc[5],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        }else if (remainBlood > 10) {
            System.out.printf(injureds_desc[6],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        }else  {
            System.out.printf(injureds_desc[7],role.getName());
            System.out.printf(",还剩下%d点血量",remainBlood);
        }
        System.out.println();
    }
    public  void showRoleInfo(){
        System.out.println("姓名为:"+getName());
        System.out.println("血量为:"+getBlood());
        System.out.println("性别为:"+getGender());
        System.out.println("长相为:"+getFace());
    }
}

测试类

public class GameTest {
    public static void main(String[] args) {
        //1.创建第一个角色
        Role r1 = new Role("乔峰",100,'男');
        //2.创建第二个角色
        Role r2 = new Role("鸠摩智",100,'男');
        r1.showRoleInfo();
        r2.showRoleInfo();

        //3.开始格斗 回合制游戏
        while(true){
            //r1开始攻击r2
            r1.attack(r2);
            //判断r2的剩余血量
            if(r2.getBlood()==0){
                System.out.println(r1.getName()+"K.O了"+r2.getName());
                break;
            }

            //r2开始攻击r1
            r2.attack(r1);
            if(r1.getBlood()==0){
                System.out.println(r2.getName()+"K.O了"+r1.getName());
                break;
            }
        }
    }
}

在这里插入图片描述

二、对象数组练习
1.定义数组存储3个商品对象
商品属性:商品的id,名字,价格,库存。
需求:创建三个商品对象,并把商品对象存入到数组当中

代码思路:
首先创建Goods类封装商品的核心属性(ID、名称、价格、库存),然后在测试类中实例化三个具体的商品对象(华为P40、一加13和惠普16),将这些对象存储到长度为3的商品数组中,最后通过遍历数组逐行输出完整的商品信息(ID+名称+价格+库存)

JavaBean类

public class Goods {
    private String id;
    private String name;
    private double price;
    private int count;

    public Goods(){}

    public Goods(String id, String name, double price, int count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

测试类

public class GoodsTest {
    public static void main(String[] args) {
        //1.创建一个数组
        Goods[] arr = new Goods[3];
        
        //2.创建三个商品对象
        Goods g1 = new Goods("1","华为P40",5999.9,100);
        Goods g2 = new Goods("2","一加13",3999.9,50);
        Goods g3 = new Goods("3","惠普16",6999.9,10);

        //3.将商品添加到数组中
        arr[0] = g1;
        arr[1] = g2;
        arr[2] = g3;
        
        //4.遍历
        for (int i = 0; i < arr.length; i++) {
            Goods goods = arr[i];
            System.out.println(goods.getId()+","+goods.getName()+","+
                    goods.getPrice()+","+goods.getCount());
        }
    }
}

在这里插入图片描述

2.定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同学生的属性:学号,姓名,年龄
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
要求2:添加完毕之后,遍历所有学生信息
要求3:通过id删除学生信息,如果存在则删除,如果不存在则提示删除失败
要求4:删除完毕之后,遍历所有学生的信息
要求5:查询数组id为"2"的学生,如果存在,则将他的年龄+1岁

代码思路见注释
JavaBean类略

//要求1-2
public class StudentTest {
    public static void main(String[] args) {
    /*定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同
     学生的属性:学号,姓名,年龄 */
    //要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
    //要求2:添加完毕之后,遍历所有学生信息
    //1.创建一个数组用来存储学生对象
    Student[] arr = new Student[3];

    //2.创建学生对象并添加到数组中
    Student stu1 = new Student(1,"zhangsan",23);
    Student stu2 = new Student(2,"lisi",24);
    Student stu3 = new Student(3,"wangwu",25);

    arr[0] = stu1;
    arr[1] = stu2;
    arr[2] = stu3;

    //要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
    Student stu4 = new Student(4,"zhaoliu",26);

    //唯一性判断
    //已存在 ---不用添加
    //不存在 ---就可以把学生对象添加进数组

    boolean flag = contains(arr, stu4.getId());
    if(flag){
        //已存在 --- 不用添加
        System.out.println("当前id重复,请修改后再添加");
    }else{
        //不存在 ---就可以把学生对象添加进数组
        //把stu4添加到数组当中
        //1.数组已经存满 --- 只能创建一个新的数组,新数组的长度 = 老数组 + 1
        //2.数组没有存满 --- 直接添加
        int count = getCount(arr);
        if(count == arr.length){
            //已经存满
            //创建一个新的数组,新数组的长度 = 老数组 + 1
            //老数组元素拷贝到新数组
            Student[] newArr = creatNewArr(arr);
            newArr[count] = stu4;
            //要求2:添加完毕之后,遍历所有学生信息
            printArr(newArr);

        }else{
            //没有存满
            arr[count] = stu4;
            //要求2:添加完毕之后,遍历所有学生信息
            printArr(arr);
        }
    }



    }

    //遍历数组
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }
        }
    }



    //创建一个新的数组,新数组的长度 = 老数组 + 1
    //老数组元素拷贝到新数组
    public static Student[] creatNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;

    }
    //定义一个方法判断数组中已经存在了几个元素
    public static int getCount(Student[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }

    //1.我要干嘛?  唯一性判断
    //2.我干这件事情,需要什么才能完成?  数组 id
    //3.调用处是否需要继续使用方法的结果?  需要
    public static boolean contains(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //依次获取到数组里的每一个学生对象
            Student stu = arr[i];
            //只有在数组满的时候才判断,否则报错
            if(stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
        }
        return false;
    }
}

在这里插入图片描述

//要求3,4
public class StudentTest1 {
    public static void main(String[] args) {
        //要求3:通过id删除学生信息,如果存在则删除,如果不存在则提示删除失败
        //要求4:删除完毕之后,遍历所有学生的信息
      
        //1.创建一个数组用来存储学生对象
        Student[] arr = new Student[3];

        //2.创建学生对象并添加到数组中
        Student stu1 = new Student(1,"zhangsan",23);
        Student stu2 = new Student(2,"lisi",24);
        Student stu3 = new Student(3,"wangwu",25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        //要求3:通过id删除学生信息,如果存在则删除,如果不存在则提示删除失败
        int index = getIndex(arr,2);
        if(index >= 0){
            //如果存在,则删除
            arr[index] = null;
            //要求4:删除完毕之后,遍历所有学生的信息
            printArr(arr);
        }else{
            //如果不存在,提示删除失败
            System.out.println("当前id不存在,删除失败");
        }
    }

    //找到id在数组中的索引
    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        //当循环找到后,还没有找到
        return -1;
    }
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }
        }
    }
}

在这里插入图片描述

//要求5
public class StudentTest2 {
    public static void main(String[] args) {
        /*定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同
     学生的属性:学号,姓名,年龄 */
        //要求5:查询数组id为"2"的学生,如果存在,则将他的年龄+1岁
        //1.创建一个数组用来存储学生对象
        Student[] arr = new Student[3];

        //2.创建学生对象并添加到数组中
        Student stu1 = new Student(1, "zhangsan", 23);
        Student stu2 = new Student(2, "lisi", 24);
        Student stu3 = new Student(3, "wangwu", 25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        //先找到id为2的索引
        int index = getIndex(arr, 2);

        //判断索引
        if(index >= 0){
            //存在,则将他的年龄+1岁
            Student stu = arr[index];
            int newAge = stu.getAge()+1;
            stu.setAge(newAge);
            printArr(arr);
        }

    }
    //找到id在数组中的索引
    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        return -1;
    }
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu!=null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值