Java巅峰之路---基础篇---综合练习(面向对象)

目录

文字版格斗游戏

基础版

souf输出语句

进阶版

键盘录入的说明

复杂对象数组练习

需求:

添加和遍历

删除和遍历

修改和遍历


文字版格斗游戏

基础版

格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new 对象的时候),这些信息就应该被确定下来。

思路:首先需要创建一个Role类,成员变量为姓名和血量以及写出对应的set和get方法,这里还需要有一个attack(Role r1, Role r2)方法,谁打了谁,谁又掉血,根据封装思想的原则:对象代表什么(人物类),就得封装对应的数据(姓名和血量),并提供数据对应的行为,所以这个行为要写在Role类里边。

package CeShi;
import java.util.Random;
public class Role {
    private String name;
    private int blood;
    Role(){}

    Role(String name, int blood){
        this.blood = blood;
        this.name = name;
    }

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

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

    /*
    * 根据封装的思想,人物攻击别人,以及损失血量都要在人物类中完成
    * r1攻击r2,r1.attack(r2);表示r1攻击r2
    * 谁攻击谁?方法的调用者去攻击参数
    * */
    public void attack(Role role){
        Random r = new Random();
        int hurt = r.nextInt(20) + 1;//造成的伤害

        //剩余血量
        int remain_blood = (role.getBlood() - hurt) > 0 ?(role.getBlood() - hurt) : 0;
        role.setBlood(remain_blood);

        System.out.println(this.name+"举起拳头打了" + role.getName()+ "一下,造成了"+ hurt+"点伤害," +
                role.getName()+"还剩下"+role.getBlood()+"点血");
    }

}
package CeShi;

public class GameText {
    public static void main(String[] args) {
        Role role1 = new Role("乔峰",100);
        Role role2 = new Role("鸠摩智",100);
        while(true){
            role1.attack(role2);
            if(role2.getBlood() == 0){
                System.out.println(role1.getName() + "K.O了" + role2.getName());
                break;
            }
            role2.attack(role1);
            if(role1.getBlood() == 0){
                System.out.println(role2.getName() + "K.O了" + role1.getName());
                break;
            }
        }
    }
}

souf输出语句

包含两部分参数
第一部分参数:要输出的内容%s(占位)
第二部分参数:填充的数据

    public static void main(String[] args) {
        System.out.printf("你好啊%s","张三\n");//你好啊张三
        System.out.printf("%s你好啊%s","张三", "李四");//张三你好啊李四
    }

进阶版

 思路:长相设置为随机,这时需要根据性别在setFace方法中写相关逻辑,同时有参构造方法中不用加长相的变量,直接用setFace方法就行。

在展示攻击效果以及受伤描述时,采用了数组以及souf输出语句;展示攻击效果用的是随机索引,受伤描述则根据人物剩余血量。

package CeShi1;

import java.util.Random;

public class Role {
    private String name;
    private int blood;

    private char gender;

    private String face;//随机长相

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

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

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

    Role(){}

    Role(String name, int blood,char gender){
        this.blood = blood;
        this.name = name;
        this.gender = gender;
        setFace(gender);
    }


    public char getGender() {
        return gender;
    }

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

    public String getFace() {
        return face;
    }

    public void setFace(char gender) {
        if(gender == '男'){
            //从boyfaces数组中选取
            Random r =  new Random();
            this.face = boyfaces[r.nextInt(boyfaces.length)];
        } else if (gender == '女') {
            //从girlfaces数组中选取
            Random r =  new Random();
            this.face = girlfaces[r.nextInt(girlfaces.length)];
        }else {
            this.face = "面目狰狞";
        }
    }


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

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

    /*
    * 根据封装的思想,人物攻击别人,以及损失血量都要在人物类中完成
    * r1攻击r2,r1.attack(r2);表示r1攻击r2
    * 谁攻击谁?方法的调用者去攻击参数
    * */

    public void attack(Role role){
        Random r = new Random();
        //攻击效果描述
        String kungFu = attacks_desc[r.nextInt(attacks_desc.length)];
        System.out.printf(kungFu,this.name,role.name);
        System.out.println();


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

        //剩余血量
        int remain_blood = (role.getBlood() - hurt) > 0 ?(role.getBlood() - hurt) : 0;
        role.setBlood(remain_blood);

        //受伤效果描述
        if(remain_blood >= 90){
            System.out.printf(injureds_desc[0],role.name);
        } else if (remain_blood >= 80 && remain_blood < 90) {
            System.out.printf(injureds_desc[1],role.name);
        } else if (remain_blood >= 70 && remain_blood < 80) {
            System.out.printf(injureds_desc[2],role.name);
        } else if (remain_blood >= 60 && remain_blood < 70) {
            System.out.printf(injureds_desc[3],role.name);
        } else if (remain_blood >= 40 && remain_blood < 60) {
            System.out.printf(injureds_desc[4],role.name);
        } else if (remain_blood >= 20 && remain_blood < 40) {
            System.out.printf(injureds_desc[5],role.name);
        } else if (remain_blood >= 10 && remain_blood < 20) {
            System.out.printf(injureds_desc[6],role.name);
        } else if (remain_blood >= 0 && remain_blood < 10) {
            System.out.printf(injureds_desc[7],role.name);
        }
        System.out.println();
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + '\'' +
                ", blood=" + blood +
                ", gender=" + gender +
                ", face='" + face + '\'' +
                '}';
    }
}
package CeShi1;

public class GameText {
    public static void main(String[] args) {
        Role role1 = new Role("乔峰",100,'男');
        Role role2 = new Role("鸠摩智",100,'男');
        System.out.println(role1.toString());
        System.out.println(role2.toString());

        while(true){
            role1.attack(role2);
            if(role2.getBlood() == 0){
                System.out.println(role1.getName() + "K.O了" + role2.getName());
                break;
            }
            role2.attack(role1);
            if(role1.getBlood() == 0){
                System.out.println(role2.getName() + "K.O了" + role1.getName());
                break;
            }

        }

    }
}

键盘录入的说明

第一套体系

nextInt()、nextDouble()、next():遇到空格、制表符、回车就停止接受,这些符号后面的数据就不会接受了

第二套体系

nextLine():可以接收空格、制表符,遇到回车才停止接受数据

        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        System.out.println(num1);
        System.out.println(num2);

        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();
        System.out.println(s);

复杂对象数组练习

需求:

定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。学生的属性:学号,姓名,年龄。

· 再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。

· 添加完毕之后,遍历所有学生信息

· 通过id删除学生信息;如果存在,则删除;不存在,则提示删除失败

· 删除完毕之后,遍历所有学生信息

· 查询数组id为"heima002"的学生,如果存在,则将他的年龄+1岁。

初步思路:根据题目要求,我们需要先创建一个Student类(标准的javabin类)和一个能放3名学生对象的数组,存储1~3个数据;然后再依次完成下列五个要求

添加和遍历

思路:在添加之前,我们需要先进行唯一性判断,若存在,则添加失败;反之,开始进行添加操作。不过,因为我们是数组存储数据,而我们定义的数组最大容量为3,所以我们还需要判断数组是否填满,填满则需进行扩容操作,没填满就根据索引将该学生对象添加进数组即可。

之后遍历数组即可

package CeShi3;

public class StudentText {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        arr[0] = new Student("1","zhangsan",23);
        arr[1] = new Student("2", "lisi", 24);
        arr[2] = new Student("3", "wangwu", 25);


        //添加一个学生对象,并在添加的时候进行学号的唯一性判断
        Student stu = new Student("4","zhaoliu", 26);
        if(contains(arr,stu)){
            //表示学生id已经存在
            System.out.println("学生id已经存在,请重新输入");
        }else {
            //表示学生id不存在
            //此时还需判断数组是否已经添加满
            int count = getCount(arr);
            if(count == arr.length){
                //表示数组已经添加满
                arr = createNewArr(arr);
                arr[count] = stu;
                System.out.println("添加成功");
                printArr(arr);
            }else {
                //表示数组还未添加满
                arr[count] = stu;
                System.out.println("添加成功");
                printArr(arr);
            }
        }
    }

    private static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                System.out.println(arr[i]);
            }
        }
    }

    //数组扩容,即构建一个新的数组
    public static Student[] createNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //获取数组中的学生数目(遇到null之前或者完整遍历arr时),添加新学生时也可作为其索引
    public static int getCount(Student[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null)count++;
            else return count;
        }
        return count;
    }

    //判断该学生学号是否已经存在
    public static boolean contains(Student[] arr, Student stu){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                if(arr[i].getId() == stu.getId())return true;
            }
        }
        return false;
    }
}

删除和遍历

思路:首先要判断该学生对象是否存在(根据id),若存在,则根据对应的索引进行删除操作;若不存在,则提示“删除失败”。“删除成功”之后,遍历数组就行

package CeShi3;

public class StudentText1 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        arr[0] = new Student("1","zhangsan",23);
        arr[1] = new Student("2", "lisi", 24);
        arr[2] = new Student("3", "wangwu", 25);


        //删除id为2的学生
        int index = getIndex(arr,"2");
        if(index >= 0){
            //学生对象存在
            System.out.println("删除成功");
            arr[index] = null;
            printArr(arr);
        }else{
            //学生对象不存在
            System.out.println("删除失败");
        }
    }
    //获取要删除学生在数组中的索引,没有则返回-1
    public static int getIndex(Student[] arr, String id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                if(arr[i].getId() == id)return i;
            }
        }
        return -1;
    }
    private static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                System.out.println(arr[i]);
            }
        }
    }
}

修改和遍历

思路:先判断该学生id是否存在,不存在,则“查询失败”;若存在,则需要将年龄加一

package CeShi3;

public class StudentText2 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        arr[0] = new Student("1","zhangsan",23);
        arr[1] = new Student("2", "lisi", 24);
        arr[2] = new Student("3", "wangwu", 25);

        //对id为2的学生进行修改操作
        int index = getIndex(arr,"5");
        if(index >= 0){
            //存在
            System.out.println("修改成功");
            int age  = arr[index].getAge() + 1;
            arr[index].setAge(age);
            printArr(arr);
        }else {
            //不存在
            System.out.println("该学生id不存在,修改失败");
        }
    }
    //获取要修改学生在数组中的索引,没有则返回-1
    public static int getIndex(Student[] arr, String id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                if(arr[i].getId() == id)return i;
            }
        }
        return -1;
    }
    private static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                System.out.println(arr[i]);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值