一。
package java2;
import java.util.Random;
/**
* 玩家类
* 属性:名字 类型 血量 防御值 攻击力
* 方法: 自我介绍 pk
*/
public class Demo4 {
//封装 :把属性设为私有 提供公共的get和set方法简介访问,提高安全性
private String name;//姓名
private String type;//类别 :战士 法师
private int life;//生命
private int degense;//防御
private int attack;//攻击力
Random a=new Random();
Random b=new Random();
Random c=new Random();
public void say(){
System.out.println("我叫"+name+",是一个"+type);
System.out.println(",生命值高达"+life+",防御值"+degense+",攻击力"+attack);
}
/**
* 我方开启的战斗
*
* @param p 敌对玩家
*/
public void pk(Demo4 p){
int flag=0;//默认我方先攻击
while (true){
//显示战斗人员的信息
this.say();
p.say();
//定义一个标记 0我方攻击 1敌方攻击
if (flag==0) {
//战斗 我方攻击力-敌方防御力=伤害
int harm = this.attack - p.degense;
int t = b.nextInt(2);
if (t!=0){
System.out.println(p.name + "掉血" + harm);
p.life -= harm;
}else{
System.out.println("伤害产生了暴击");
System.out.println(p.name + "掉血" + harm*2);
p.life -= harm*2;
}
flag=1;
}else{
//战斗 敌方攻击力-我方防御力=伤害
int harm=p.attack-this.degense;
int r=c.nextInt(2);
if (r!=0){
System.out.println(this.name + "掉血" + harm);
this.life -= harm;
}else{
System.out.println("伤害产生了暴击");
System.out.println(this.name + "掉血" + harm*2);
this.life -= harm*2;
}
flag=0;
}
//直到一方死亡
//血量《=0,战斗结束
if(this.life<0){
System.out.println(p.name+"打败了"+this.name);
break;
}
if (p.life<0){
System.out.println(this.name+"打败了"+p.name);
break;
}
//线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String []str={"屠龙宝刀","绿毒裁决","鸟盾","自宫刀","快🗡","电刀","闪电","黄刀","2金币","3金币","4金币","5金币","6金币","7金币","8金币","9金币","10金币","100金币","1金币"};
for (int x=0;x<str.length;x++){
x=a.nextInt(19);
System.out.println("恭喜你获得一件:"+str[x]);
break;
}
}
//构造器(如果定义有参的构造器 一定定义一个无参的构造器)
public Demo4(String name, String type, int life, int degense, int attack) {
this.name = name;
this.type = type;
this.life = life;
this.degense = degense;
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 getDegense() {
return degense;
}
public void setDegense(int degense) {
this.degense = degense;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
}
package java2;
/**
* 测试玩家类
* 创建玩家对象pk
*/
public class Playertest {
public static void main(String[] args) {
Demo4 p1 = new Demo4("龙奶妈", "法师", 100, 25, 35);
Demo4 p2 = new Demo4("龙天天", "师法", 70, 12, 40);
//战斗
p1.pk(p2);
}
}
二。
package java2;
public class Counter {
private Goods[]goodses = new Goods[10];
int num=10;
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void show(){
for (Goods gd: goodses){
if (gd !=null){
System.out.println(gd);
}
}
}
}
package java2;
public class CounterTest {
public static void main(String[] args) {
Counter counter=new Counter();
counter.show();;
}
}
package java2;
/**
* 实体类:Goods
* 属性
* id 商品编号
* goodsName 商品名称
* price 商品价格
* desc 商品描述
*/
public class Goods {
private int id;
private String goodsName;
private int price;
private String 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 "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}