玩家类pk
增加暴击
增加物品掉落(击杀敌人后随机掉落物品)
private String name;//名字
private String kind;// 类型
private Integer sm;// 生命
private Integer fy;//防御
private Integer gj;//攻击
public xuanze(String name, String kind, Integer sm, Integer fy, Integer gj) {
this.name = name;
this.kind = kind;
this.sm = sm;
this.fy = fy;
this.gj = gj;
}
//描述自己属性
public void say(){
System.out.print("我叫"+name+",是一个"+kind);
System.out.println(",生命值高达"+sm+",防御值"+fy+",攻击"+gj);
}
//我方开启PK 敌对玩家
public void pk(xuanze p){
//暴击倍数
int[] a=new int[]{1,1,1,1,2,2,3,3};
//暴击装备
String[] b=new String[]{"布甲","大剑","腰带"};
//定义一个标记,0我方攻击,1地方攻击
int flag=0;
//直到一方死亡,就结束
while(true){
Random random=new Random();
//显示当前战斗人员信息
this.say();
p.say();
if(flag==0){
//暴击
int bj =a[random.nextInt(a.length)];
//掉血
int harm=this.gj*bj-p.fy;
System.out.println(p.name+"掉血"+harm);
p.sm-=harm;
flag=1;
}else{
//暴击
int bj=a[random.nextInt(a.length)];
int harm=p.gj*bj-this.fy;
System.out.println(this.name+"掉血"+harm);
this.sm-=harm;
flag=0;
}
//血量<=0,战斗结束
if(this.sm<=0){
System.out.println(p.name+"打败了"+this.name);
System.out.println(b[random.nextInt(b.length)]);
break;
}
if(p.sm<=0){
System.out.println(this.name+"打败了"+p.name);
System.out.println(b[random.nextInt(b.length)]);
break;
}
//线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public Integer getSm() {
return sm;
}
public void setSm(Integer sm) {
this.sm = sm;
}
public Integer getFy() {
return fy;
}
public void setFy(Integer fy) {
this.fy = fy;
}
public Integer getGj() {
return gj;
}
public void setGj(Integer gj) {
this.gj = gj;
}
public xuanze() {
}
.柜台商品管理系统
实体类:Goods
属性
id 商品编号
goodsName 商品名称
price 商品价格
desc 商品描述
封装..提供get和set
无参构造器和有参构造器
有参构造器初始化初始化商品对象
重写toString()方法
可以直接显示数据
柜台类:Counter
属性:
柜台商品列表,固定10个商品位置
num 柜台商品数量
构造器:
无参构造器初始化2个商品
业务方法:
展示柜台所有的商品(不能输出null)
测试类:CounterTest
程序入口main方法中,创建柜台对象,调用show()方法展示柜台商品
package com.hp.goods;
public class Goods {
private Integer id;
private String name;
private double prices;
private String desc;
public Goods() {
}
public Goods(Integer id, String name, double prices, String desc) {
this.id = id;
this.name = name;
this.prices = prices;
this.desc = desc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrices() {
return prices;
}
public void setPrices(double prices) {
this.prices = prices;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", prices=" + prices +
", desc='" + desc + '\'' +
'}';
}
}
package com.hp.goods;
public class Counter {
private int num;
Goods[] goods=new Goods[10];
public Counter(){
this.goods[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goods[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void show(){
for (int i = 0; i <goods.length; i++) {
if(goods[i]!=null){
System.out.println(goods[i]);
}
}
}
}
package com.hp.goods;
public class CounterTest {
public static void main(String[] args) {
Counter ps=new Counter();
ps.show();
}
}