java-面向对象综合练习 备份

 1、文字格斗回合制游戏

需求:

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

举例:

程序运行之后结果为:

姓名为:乔峰 血量为:100

姓名为:鸠摩智 血量为:100

乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。

鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。

乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。

鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。

乔峰K.O.了鸠摩智

public class TextCombat01 {
    public static void main(String[] args) {
        Role r1 = new Role("乔峰", 120);
        Role r2 = new Role("鸠摩智", 100);
        while(true){
            r1.attack(r2);
            if(r2.getBlood() == 0){
                System.out.println(r1.getName() + "KO了" + r2.getName());
                break;
            }
            r2.attack(r1);
            if(r1.getBlood() == 0){
                System.out.println(r2.getName() + "KO了" + r1.getName());
            }
        }

    }
}
class Role {
    private String name;//姓名
    private int blood;//血量

    public Role() {
    }

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

    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 random = new Random();
        //造成伤害的血量
        int hurt_blood = random.nextInt(20) + 1;
        //角色剩余血量
        int blood = role.getBlood() - hurt_blood;
        //判断血量是否为空
        blood = blood < 0 ? 0 : blood;
        //设置被攻击者的血量
        role.setBlood(blood);
        //this方法表示调用者
        System.out.println(this.name + "攻击了," + role.getName() + ",造成了"+hurt_blood+ "点伤害,"
        + role.getName() + " 剩余血量是 " + role.getBlood());

    }
}

运行结果:

2、数组对象综合练习

需求:

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

学生的属性:学号,姓名,年龄。

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

要求2:添加完毕之后,遍历所有学生信息。

要求3:通过id删除学生信息

如果存在,则删除,如果不存在,则提示删除失败。

要求4:删除完毕之后,遍历所有学生信息。

要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁

public class ArrayObject {
    public static void main(String[] args) {
        Student[] newArray = null;
        //定义一个数组
        Student[] students = new Student[3];
        //定义两个学生对象
        Student s1 = new Student(1, "zhangsan", 18);
        Student s2 = new Student(2, "lisi", 20);
        Student s3 = new Student(3, "lisi", 20);
        //将对象放入数组中
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        Student s4 = new Student(4, "wangwu", 21);
        //根据学号判断是否存在
        boolean flag = contain(students, s4);
        //System.out.println(flag);
        if(flag){
            //首先判断数组是否已满,如果满了需要创建一个新的数组,长度为当前数组长度+1
            int count = getCount(students);
            if(count == students.length){
                //创建一个新的数组,长度为当前长度+1
                newArray = getNewArray(students);
                newArray[newArray.length - 1] = s4;
            }
            //students[2] = s3;
        }else{
            System.out.println("学号:"+s4.getCode() + "已存在");
        }
        //遍历数组
        forAll(newArray);
        //判断数组中是否有code
        int code = 9;
        boolean isHaving = false;
        Student[] new_arr = null;
        for (int i = 0; i < newArray.length; i++) {
            if(newArray[i] != null){
                if(newArray[i].getCode() == code){
                    //删除
                    new_arr = deleteArrayById(code, newArray);
                    isHaving = true;
                    break;
                }
            }
        }
        if(!isHaving){
            System.out.println("删除失败");
        }else{
            System.out.println("遍历新数组");
            forAll(new_arr);
        }

        Student[] a1 = selectById(2, newArray);
        forAll(a1);
    }

    public static Student[] selectById(int code,Student[] array){
        for (int i = 0; i < array.length; i++) {
            if(array[i] != null){
                if(array[i].getCode() == code){
                    int age = array[i].getAge();
                    array[i].setAge(age + 1);
                    break;
                }
            }
        }
        return array;
    }

    public static Student[] deleteArrayById(int code,Student[] array){

        Student[] new_arr = new Student[array.length - 1];
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] != null){
                if(array[i].getCode() !=  code){
                    new_arr[count] = array[i];
                    count ++;
                    continue;
                }
            }
        }

        return new_arr;
    }

    public static Student[] getNewArray(Student[] arr){
        int old_len = arr.length;
        Student[] newArr = new Student[old_len + 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;
    }
    public static void removeByCode(int code,Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                if(code == arr[i].getCode()){
                    // arr.
                }
            }
        }
    }
    public static void forAll(Student[] students){
        for (int i = 0; i < students.length; i++) {
            if(students[i] != null){
                System.out.println("学号:" + students[i].getCode()+";姓名:"+students[i].getName()+";年龄:" + students[i].getAge());
            }

        }
    }
    //根据学号判断数组中是否存在
    public static boolean contain(Student[] students,Student student){
        boolean flag = true;
        int code = student.getCode();
        for (int i = 0; i < students.length; i++) {
            Student s1 = students[i];
            if(s1 != null){
                if(s1.getCode() == code){
                    flag = false;
                    break;
                }
            }

        }
        return flag;
    }
}
//定义学生对象
class Student{
    private int code;
    private String name;
    private int age;

    public Student(int code, String name, int age) {
        this.code = code;
        this.name = name;
        this.age = age;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3、字符串练习1

 需求:

键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
public class CountChars {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //请输入字母或者数字
        String contents = sc.next();
        //大写字母数量
        int upCount = 0;
        //小写字母数量
        int lowerCount = 0;
        //数字数量
        int numCount = 0;
        for (int i = 0; i < contents.length(); i++) {
            char c = contents.charAt(i);
            if(c >= 'a' && c <= 'z'){
                lowerCount ++;
            }else if(c >= 'A' && c <= 'Z'){
                upCount ++;
            }else if(c >= '0' && c <= '9'){
                numCount ++;
            }
        }
        System.out.println("大写字母个数:" + upCount);
        System.out.println("小写字母个数:" + lowerCount);
        System.out.println("数字个数:" + numCount);
    }
}

4、字符串练习2

需求:

定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法, 

并在控制台输出结果。例如,数组为 int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3]

public class StringTest01 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        StringBuffer test = test(arr);
        System.out.println(test.toString());
    }

    public static StringBuffer test(int[] arr){
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);

            if(i < arr.length - 1){
                sb.append(",");
            }

        }
        sb.append("]");
        return sb;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geminigoth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值