【无标题】

package com.alibaba.demo2;

import java.util.Random;

/**
 * 玩家类
 * 属性,名字,类型,生命值,防御值,攻击力
 *方法:自我介绍、pk
 */
   public class Player {
       //封装:把属性设为private,提供公共的get和set方法简介访问,提高安全性
    private String name;//姓名
    private String type;//类型;战士、法师
    private int life; //生命值
    private int defense;//防御值
    private int attack;//攻击力

    /**
     * 描述自己的属性
     *
     */


    public void say(){
        System.out.println("我叫"+name+",是一个"+type);
        System.out.println(",生命值高达"+life+",防御值"+defense+",攻击力"+attack);

    }

    /**
     * 我方开启的战斗
     * @param p 敌对玩家
     */
    public  void pk(Player p) {
        int flag = 0;
        while (true) {
            this.say();
            p.say();
            if (flag == 0) {
                Random r = new Random();
                int a =(int) r.nextInt(2)+1;
                // 随机产生一个整数
                int harm = (this.attack - p.defense)*a;
                if (a==2){
                    System.out.println(p.name+"暴击了"+this.name + "掉血" + harm);
                }else {
                    System.out.println(this.name + "掉血" + harm);
                }

                this.life -= harm;
                flag = 1;
            } else {
                Random r = new Random();
                int b=(int) r.nextInt(2)+1;
                // 随机产生一个整数
                int harm = (this.attack - p.defense)*b;
                if (b==2){
                    System.out.println(this.name+"暴击了"+p.name + "掉血" + harm);
                }else {
                    System.out.println(p.name + "掉血" + harm);
                }
                p.life -= harm;
                flag = 0;
            }
            if (this.life <= 0) {

                System.out.println(p.name + "打败了" + this.name);
                Random r = new Random();
                int b=(int) r.nextInt(2)+1;
                if (b==2){
                    System.out.println("掉落了武器");
                }else {
                    System.out.println("掉落了防具");
                }
                break;
            }
            if (p.life <= 0) {
                System.out.println(this.name + "打败了" + p.name);
                Random r = new Random();
                int b=(int) r.nextInt(2)+1;
                if (b==2){
                    System.out.println("掉落了武器");
                }else {
                    System.out.println("掉落了防具");
                }
                break;
            }

        }
    }




    //构造器<建议如果定义了有参的构造器, 一定定义一个无参的构造器>


    public Player(String name, String type, int life, int defense, int attack) {
        this.name = name;
        this.type = type;
        this.life = life;
        this.defense = defense;
        this.attack = attack;
    }



    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }
}
package com.alibaba.demo2;

/**
 * 测试玩家类
 * 创建玩家对象pk
 */
public class PlayerTest {
    public static void main(String[] args) {
        Player p1 = new Player("寒冰","射手",100,20,25);
        Player p2 = new Player("发条","法师",200,10,15);


        p1.pk(p2);


    }
}

package com.alibaba.zuoye;

public class Goods {
       private int id;
       private String goodsname;
       private int price;
       private String desc;

    public int getId() {
        return id;
    }

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

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public int getPrice() {
        return price;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Goods(int id, String goodsname, int price, String desc) {
        this.id = id;
        this.goodsname = goodsname;
        this.price = price;
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Counter{" +
                "id=" + id +
                ", goodsname='" + goodsname + '\'' +
                ", price=" + price +
                ", desc='" + desc + '\'' +
                '}';
    }
}
package com.alibaba.zuoye;

public class Counter {
        Goods[]goodses = new Goods[10];
        int num = 0;
        public Counter(){
            this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备");
            this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了");
            num=2;//相当于两个商品

        }
    public void show(){
        for (int i = 0; i < goodses.length; i++) {
            if (i<num){
                System.out.println(goodses[i]);
            }
        }
    }
}

package com.alibaba.zuoye;

public class CounterText {
                public static void main(String[] args) {
                        Counter c = new Counter();
                        c.show();
                }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值