java09面向对象综合练习

1.文字格斗

import java.util.Random;
    public class Role{
        private String name;
        private int blood;
        private  char gender;
        private String characteristic;

        String[] a = {"A","B","C","D","E","F"};
        String[] b = {"a","b","c","d","e","f"};

        String[] attack_desc={
                "%s使出了一招背心顶,钻到对方身后,一掌像%s背心打去",
                "%s打出了###,抓向%s",
                "%s大喊一声,推向%s"
        };
        String[] injure_desc={
                "%s退了半步,毫发无伤",
                "%s脸色惨白",
                "%s连退几步",
                "%s倒了下去"
        };
        public char getGender() {
            return gender;
        }

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

        public String getCharacteristic() {
            return characteristic;
        }

        public void setCharacteristic(char gender) {
            Random r = new Random();

            if (gender == '男'){
                int index = r.nextInt(a.length);
                this.characteristic=a[index];
            } else if(gender =='女'){
                int index = r.nextInt(b.length);
                this.characteristic=b[index];
            }else {
                this.characteristic="G";
            }

        }

        public Role() {
        }
        public Role(String name, int blood,char gender) {
            this.name = name;
            this.blood = blood;
            this.gender = gender;
            //性格是随机的
            setCharacteristic(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(attack_desc.length);
            String kungfu = attack_desc[index];

            System.out.printf(kungfu,this.getName(),role.getName());
            System.out.println();
            //this表示方法的调用者
            //计算伤害

            int hurt = r.nextInt(20)+1;

            //剩余血量
            int remainBlood = role.getBlood()-hurt;
            //验证血量是否为负数
            remainBlood = remainBlood < 0 ? 0:remainBlood;
            //修改血量
            role.setBlood(remainBlood);

//            System.out.println(this.getName()+"打了"+role.getName()+
//                    "下"+",造成了"+hurt+"点伤害,"+role.getName()+"的血量还剩下"+role.getBlood());

            //受伤的描述
            if(remainBlood >75){
                System.out.printf(injure_desc[0],role.getName());
            }else if(remainBlood >50 &&remainBlood <=75){
                System.out.printf(injure_desc[1],role.getName());
            }else if(remainBlood >25 &&remainBlood <=0){
                System.out.printf(injure_desc[2],role.getName());
            }else{
                System.out.printf(injure_desc[3],role.getName());
            }
            System.out.println();
        }
        public void showRoleInfo(){
            System.out.println("姓名:"+getName());
            System.out.println("血量:"+getBlood());
            System.out.println("性别:"+getGender());
            System.out.println("特点:"+getCharacteristic());
        }

    }

public class Gametest {
    public static void main (String[] args){
    Role r1 = new Role("小红",100,'女');
    Role r2 = new Role("小明",100,'男');
    r1.showRoleInfo();
    r2.showRoleInfo();
    //开始格斗,回合制游戏
        while(true){
            //r1开始攻击r2
            r1.attack(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;
            }
        }
    }
}

2.对象数组

        键盘录入:

A    a.nextInt();接受整数  b.nextDouble();接受小数    c.next();接受字符串  遇到空格、制表符、回车这些符号,就不会接受了

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字");
        int a = sc.nextInt();
        System.out.println(a);
        System.out.println("请输入第二个数字");
        int b = sc.nextInt();
        System.out.println(b);
        //输入123 123 则a=123 b=123
        //nextDouble(),next()一样的
    }
}

B   a.nextLine();接受字符串    可以接受空格,制表符,遇到回车才停止接受

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
       String str1 = sc.nextLine();
        System.out.println(str1);
        System.out.println("请输入第二个字符串");
        String str2 = sc.nextLine();
        System.out.println(str2);
       //输入abc ded 则str1=abc ded
    }
}

键盘录入的两套体系不能混用。

        对象数组练习1:

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;
    }
}
import java.util.Scanner;

public class GoodsTest {
    public static void main(String[] args){
        //1.创建数组
        Goods[] arr = new Goods[3];
        //2.创建三个对象
        Scanner sc =new Scanner(System.in);
        for(int i = 0;i <arr.length ;i++){
            Goods g = new Goods();//要将对象创建在循环里面,循环一次,创建一次对象
            System.out.println("请输入商品的id:");
            String id = sc.next();
            g.setId(id);
            System.out.println("请输入商品的名字:");
            String name = sc.next();
            g.setName(name);
            System.out.println("请输入商品的价格:");
            Double price = sc.nextDouble();
            g.setPrice(price);
            System.out.println("请输入商品的库存:");
            int count = sc.nextInt();
            g.setCount(count);

            //把商品对象添加到数组中
            arr[i] = g;
        }
        for(int i = 0;i<arr.length;i++){
            Goods a = arr[i];
            System.out.println(a.getId()+","+a.getName()+","+a.getPrice()+","+a.getCount());
        }
    }
}

数组对象练习2:

        a.要求1和要求2

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

        Student stu1 = new Student(1,"amy",19);
        Student stu2 = new Student(2,"sarly",19);
     // Student stu3 = new Student(3,"mark",19);

        arr[0] = stu1;
        arr[1] = stu2;
      //  arr[2] = stu3;
    //1.再次添加一个学生对象,并对其学号进行唯一判断
        Student stu4 = new Student(4,"lisa",18);
        //方法constains完成  写这个方法之前想3个问题
        //做什么? 完成唯一性判断 需要什么完成?数组和id  调用处是否需要使用这个方法的返回结果? 返回存在图不存在;
       boolean flag = constains(arr,stu4.getId());
       if(flag){
           System.out.println("当前id重复,请修改后添加");
       }else{
        //不存在的话,就把学生对象添加进数组
           //a.如果数组已经满了,只能创建一个新的数组,新的数组长度=老数组长度+1;
           //b.数组没有存满 直接添加   -----写个方法判断数组中已经存了几个元素
        int count = getcount(arr);
             if(count == arr.length){
            //已经存满了
            //创建一个新的数组,长度等于老数组+1;并且将老数组里面的元素添加到新数组中
            //写个方法实现上面的功能
                    Student[] newarr =creatarr(arr);
                    newarr[count] = stu4;
            //2.添加完毕后,遍历所有学生的信息
                    printarr(newarr);
        }else {
            //没有存满
                    arr[count]=stu4;
            2.添加完毕后,遍历所有学生的信息
                    printarr(arr);
        }
       }

    //2.添加完毕后,遍历所有学生的信息  写一个方法实现
    }
    public static boolean constains(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;//把这句话写在循环外面

    }
    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 Student[] creatarr(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 void printarr(Student [] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
    }
}

        b.要求3要求4

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

        Student stu1 = new Student(1,"amy",19);
        Student stu2 = new Student(2,"sarly",9);
        Student stu3 = new Student(3,"mark",19);

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

        //3.通过id删除学生信息,如果存在 就删除;不存在就提示删除失败
        //要找到id在数组中对应的索引 需要数组和id 需要返回索引值
       int index = getIndex(arr,3);
        //System.out.println(index);
        //进行判断
       if(index >= 0){
           //如果存在就删除
           arr[index]=null;
           printarr(arr);
       }else{
           //如果不存在,就提示删除失败
           System.out.println("当前id不存在,删除失败");
       }
       //4.查询id为2的学生,如果存在,则将他的年龄+1岁
        int index1 =getIndex(arr,2);
        if(index1>=0){
        int newage = arr[index1].getAge()+1;
        arr[index1].setAge(newage);//将+1后的年龄塞回去
        printarr(arr);
       }else{
           System.out.println("当前id不存在,修改失败");
           printarr(arr);
       }    
    }
    public static int getIndex (Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                int sid = arr[i].getId();
                if(sid == id){
                    return i;//返回索引
                }
            }
        }
        //当循环结束后还没有找到,就表示不存在
        return -1;
    }
    public static void printarr(Student [] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println(arr[i].getId() + "," + arr[i].getName() + "," + arr[i].getAge());
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值