面向对象综合练习(键盘录入,文字版格斗游戏,度睇相数组练习,求三个手机的平准价格,对象数组)

键盘录入

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

第一套体系

  • nextInt();  接受整数
  • nextDouble();  接受小数
  • next();  接受字符串

遇到空格,制表符,回车键会自动结束录入

第二套体系

nextLine();  接受字符串

遇到空格都不会结束录入,遇到回车会结束录入

文字版格斗游戏

类代码
import java.util.Random;

public class role {
    //属性
    private String name;
    private int blood;

    //空参
    public role() {
    }

    //全参
    public role(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }
    //get和set方法


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

    public String getName() {
        return name;
    }

    public int getBlood() {
        return blood;
    }

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

    //定义一个方法用于攻击别人
    public void attack(role roler) {
        //计算造成的伤害
        Random r = new Random();
        int hurt = r.nextInt(20) + 1;
        //修改一下挨揍人的血量
        //剩余血量
        int remainBlood = roler.getBlood() - hurt;
        //对剩余血量做验证,要是负数,调为0
        remainBlood=remainBlood<0?0:remainBlood;
        roler.setBlood(remainBlood);



        //this 表示方法的的调用者
        System.out.println(this.getName()+"举起拳头,打了"+roler.getName()+"一下"+
            "造成了"+hurt+"点伤害,"+roler.getName()+"还剩下了"+remainBlood+"血量"    );
    }

}
测试类代码
public class GameTest {
    public static void main(String[] args) {
        //创建第一个角色
        role r1 = new role("艾山", 100);
        role r2 = new role("麦麦", 100);
        //开始格斗回合制游戏
        while (true) {
            //1开始攻击2
            r1.attack(r2);
            if (r2.getBlood() == 0) {
                System.out.println(r1.getName()+"KO了"+r2.getName());
                break;
            }
            r2.attack(r1);
            //2开始攻击1
            if (r2.getBlood()==0){
                System.out.println("游戏结束");
                break;
            }

        }
    }
}

对象数组练习

类代码
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) {
       /* 定义数组存储三个商品
        商品的属性,ID 名字,价格,库存
        创建三个对象,把商品的对象存入到数组当中*/
        Goods[] arr = new Goods[3];
        //创建三个对象
        Goods g1 = new Goods("001", "华为手机", 3000, 210);
        Goods g2=new Goods("358","苹果手机",5000,520);
        Goods g3=new Goods("5233","小米手机",2204,1235);
        //吧商品添加到数组中
        arr[0]=g1;
        arr[1]=g2;
        arr[2]=g3;
        for (int i = 0; i < arr.length; i++) {
            Goods goods=arr[i];
            System.out.println(goods.getId()+","+goods.getName()+","+goods.getCount()+","+goods.getPrice());
        }


    }
}

求三个手机的平均价格

类代码
public class phonetest {
    public static void main(String[] args) {
        //创建手机的对象
        phone p=new phone();
        //叫做给手机赋值
        p.brand="小米";
        p.price=1530;
        //获取手机对象中的值
        System.out.println(p.brand);
        System.out.println(p.price);
        //调用手机中的方法
        p.call();
        p.playgame();

        phone pp=new phone();
        pp.brand="华为";
        pp.price=1300;
        System.out.println(pp.brand);
        System.out.println(pp.price);
        pp.playgame();
    }
}
测试代码
public class PphoneTest {
    public static void main(String[] args) {
         /* 定义数组存储3部手机对象。
    手机的属性:品牌,价格,颜色。
    要求,计算出三部手机的平均价格*/
        //创建一个数组
        Pphone[] arr = new Pphone[3];
        //创建手机对象
        Pphone p1 = new Pphone("huawe", 520, "hong");
        Pphone p2 = new Pphone("vivo", 3520, "red");
        Pphone p3 = new Pphone("oppo", 5282, "black");
        //把手机对象添加到数组当中
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;
        //求三部手机的平均价格
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Pphone phone = arr[i];//依次表示每一个手机里面的对象
            sum += phone.getPrice();
        }
//求平均数
        int avg=0;
        avg=sum/ arr.length;
System.out.println(avg);
    }

}

对象数组四

类代码
public class GrilFrined {
   /* 定义数组存储4个女朋友的对象女朋友的属性:姓名、年龄、性别、爱好
    要求1:计算出四女朋友的平均年龄
    要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来*/
    private String  name;
    private int age;
    private String gender;
    private String hobby;

    public GrilFrined() {
    }

    public GrilFrined(String name, int age, String gender, String hobby) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.hobby = hobby;
    }

    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;
    }

    public String getGender() {
        return gender;
    }

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

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}


测试代码

public class PphoneTest {
    public static void main(String[] args) {
         /* 定义数组存储3部手机对象。
    手机的属性:品牌,价格,颜色。
    要求,计算出三部手机的平均价格*/
        //创建一个数组
        Pphone[] arr = new Pphone[3];
        //创建手机对象
        Pphone p1 = new Pphone("huawe", 520, "hong");
        Pphone p2 = new Pphone("vivo", 3520, "red");
        Pphone p3 = new Pphone("oppo", 5282, "black");
        //把手机对象添加到数组当中
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;
        //求三部手机的平均价格
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Pphone phone = arr[i];//依次表示每一个手机里面的对象
            sum += phone.getPrice();
        }
//求平均数
        int avg=0;
        avg=sum/ arr.length;
System.out.println(avg);
    }

}

数组五

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值