1.玩家类pk
增加暴击
增加物品掉落(击杀敌人后随机掉落物品)
package com.hp.demo;
import java.util.Random;
public class Player {
/*
玩家类
属性,名字,类型,生命值,防御值,攻击力
方法,自我介绍,PK
*/
//封装,把属性设置为私有,提供的get 和set方法间接访问,提高安全性
private String name;
private String type;
private int life;
private int defence;
private int attack;
public void say(){
System.out.println("我叫"+name+",是一个"+type);
System.out.println(",生命值"+life+",防御值"+defence+",攻击力"+attack);
}
public void pk(Player p){
//定义一个标志,0我方攻击,1敌方攻击
int flag = 0;//默认我方先攻击
while(true){
//显示一下当前战斗人员的信息
this.say();
p.say();
if (flag==0){
//战斗 我方攻击力-敌方防御力=伤害值
int hadm = this.attack-p.defence;
//暴击
int bj = (int) Math.round(Math.random()*( 10 - 1 )+1);
if(bj== 3||bj==9) {
System.out.println("暴击了!!!"+p.name+"掉血"+hadm*2);
p.life -=hadm*2;
}else {
System.out.println(p.name+"掉血"+hadm);
p.life -=hadm;
}
//让敌方生命值-伤害值
flag = 1;//改变标记,转换攻击角色
}else{
//战斗 敌方攻击力-我方防御力=伤害值
int hadm = p.attack-this.defence;
int bj = (int) Math.round(Math.random()*( 10 - 1 )+1);
if(bj== 3||bj==9) {
System.out.println("暴击了!!!"+this.name+"掉血"+hadm*2);
this.life -=hadm*2;
}else {
System.out.println(this.name+"掉血"+hadm);
this.life -=hadm;
}
//让我方生命值-伤害值
flag =0;//改变标记,转换攻击角色
}
//有血量<=0,战斗结束
if (this.life<=0){
System.out.println(p.name+"打败了"+this.name);
this.zb();
break;
}
if (p.life<=0){
System.out.println(this.name+"打败了"+p.name);
this.zb();
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void zb(){
String[]arr = {",,,","nnnn","mmmmm","55555"};
int x = (int) Math.round(Math.random()*(arr.length-1-0)+0);
System.out.println("掉落["+arr[x]+"]! !");
}
public Player() {
}
public Player(String name, String type, int life, int defence, int attack) {
this.name = name;
this.type = type;
this.life = life;
this.defence = defence;
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 getDefence() {
return defence;
}
public void setDefence(int defence) {
this.defence = defence;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
}
实现类
package com.hp.demo;
public class PlayerTest {
/*
测试类玩家类
玩家对象pk
*/
public static void main(String[] args) {
Player p1 =new Player("张三","战士",100,20,30);
Player p2 =new Player("李四","法师",70,15,50);
new Player();
p1.pk(p2);
}
}
2.柜台商品管理系统
实体类:Goods
属性
id 商品编号
goodsName 商品名称
price 商品价格
desc 商品描述
封装..提供get和set
无参构造器和有参构造器
有参构造器初始化初始化商品对象
重写toString()方法
可以直接显示数据
柜台类: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;//相当于两个商品
}
业务方法:
展示柜台所有的商品(不能输出null)
public void show(){
}
测试类:CounterTest
程序入口main方法中,创建柜台对象,调用show()方法展示柜台商品
package com.hp.demo;
import java.lang.reflect.Constructor;
public class Goods {
private int id;
private String goodsName;
private int price;
private String desc;
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
public Goods() {
}
public Goods(int id, String goodsName, int price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = 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;
}
}