1.玩家类pk
增加暴击
增加物品掉落(击杀敌人后随机掉落物品)
package com.hp.z;
/*
* 玩家类
* 属性:名字。类型,生命值,防御值,攻击类
* 方法:自我介绍。PK
* */
public class Player {
//封装:把属性设为私有(private),提供公共的get和set方法间接访问,提高安全性
private String name;//姓名
private String type;//类型:战士,法师
private int life;//生命值
private int defense;//防御值
private int attack;//攻击力
private int bj;//暴击
private int w;
/*
*描述自己的属性
*/
public void say(){
System.out.print("我叫"+name+",是一个"+type);
System.out.print("生命值"+life+",防御值"+defense+",攻击力"+attack);
}
/*
*我方开启的战斗
* @param p 敌对玩家
*/
public void pk(Player p){
//回合制,直到一方死亡为止
while (true){
//显示一下当前战斗人员的信息
this.say();
p.say();
//定义一个标记,0我方攻击 1敌方攻击
int flag = 0;//默认我方先攻击
if (flag==0){
if (bj<=4){
int harm = (this.attack-p.defense);
p.setLife(p.life-harm);//敌方生命值减少
System.out.println("lisa回合,攻击暴击了");
System.out.println(p.name+"掉血"+harm);
}else {
//战斗 我方攻击力-敌方防御值=伤害力
int harm = this.attack - p.defense;
System.out.println(p.name + "掉血" + harm);
p.life -= harm;//敌方生命值-伤害值
}
flag=1;//改变标记,转换攻击角色
}else{
//战斗 敌方攻击力-我方防御值=伤害力
if(bj<=5){
int harm = (p.attack=this.defense)*2;
this.setLife(this.life-harm);//我方生命值减少
System.out.println("Lalisa回合,攻击暴击了");
}else {
int harm = p.defense - this.attack;
System.out.println(p.name + "掉血" + harm);
p.life -= harm;//我方生命值-伤害值
}
flag=0;//改变标记,转换攻击角色
}
//有血量<=0,战斗结束
if (this.life<=0){
System.out.println(p.name+"打败了"+this.name);
if (w==1){
System.out.println("掉落金币");
}else if (w==2){
System.out.println("掉落稀有物品包包");
}
break;//结束循环
}
else if (p.life<=0){
System.out.println(this.name+"打败了"+p.name);
if (w==1){
System.out.println("掉落一件衣服");
}else if (w==2){
System.out.println("掉落一件饰品");
}
break;
}
//线程休眠
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//构造器(建议如果定义了有参构造器,一定定义一个无参构造器)
public Player() {
}
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.hp.z;
/*
*测试玩家类
* 创建玩家对象pk
*
*/
public class PlayerTest {
public static void main(String[] args) {
//创建两个玩家对家
Player p1 = new Player("lisa","战士",1000,20,80);
Player p2 = new Player("Lalisa","法师",100,50,60);
//战斗
p1.pk(p2);
}
}
2.柜台商品管理系统
实体类:Goods
属性
id 商品编号
goodsName 商品名称
price 商品价格
desc 商品描述
public class Goods {
private int id;//商品编号
private String goodsName;//商品名称
private int price;//商品价格
private String desc;//商品描述
封装..提供get和set
无参构造器和有参构造器
有参构造器初始化初始化商品对象
重写toString()方法
可以直接显示数据
public Goods(int id, String goodsName, int price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
public Goods() {
}
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;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
}
柜台类:Counter
属性:
柜台商品列表,固定10个商品位置
Goods[]goodses = new Goods[10];
num 柜台商品数量
构造器:
无参构造器初始化2个商品
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public class Counter {
Goods[] gs = new Goods[10];
public Counter(){
this.gs[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.gs[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
}
业务方法:
展示柜台所有的商品(不能输出null)
public void show(){
}
public void show(){
int a = 0;
for (int i = 0; i < gs.length; i++) {
if (gs[i] != null){
System.out.println(gs[i]);
}else{
a++;
}
}
System.out.println("还有"+a+"个货品暂时为空");
}
测试类:CounterTest
程序入口m
package com.hp.zuoye;
public class CounterTest {
public static void main(String[] args) {
Counter counter = new Counter();
counter.show();
}
}
ain方法中,创建柜台对象,调用show()方法展示柜台商品