当前位置:网站首页>课堂练习--0708
课堂练习--0708
2022-08-11 05:12:00 【余谦吖】
课堂练习–玩家对战,击败掉装备,随机暴击率
实体类如下:
package cn.hp;
import java.util.Random;
/*
* 玩家类
* 属性:名字、类型、生命值、防御值、攻击力
* 方法:自我介绍、pk
* */
public class Player {
//封装:把属性设为私有,提供公共的get和set方法间接访问,提高安全性
private String name;
private String type;
private int life;
private int defence;
private int attack;
private int crit;
Random ra = new Random();
//随机暴击率
public int crit(){
crit = ra.nextInt(100);
return crit;
}
//攻击产生暴击伤害的几率
public int chance(){
int damage = ra.nextInt(100);
return damage;
}
//随机掉落装备
public int equipment(){
int equipment = ra.nextInt(100);
return equipment;
}
//装备爆率以及装备名称
public void zb(){
int zb = equipment();
if (zb==1){
System.out.println("恭喜您掉落SSSR武器《风过无痕--短剑》");
}else if (zb==2){
System.out.println("恭喜您掉落SSSR武器《二十四桥明月--法器》");
}else if (zb==3){
System.out.println("恭喜您掉落SSSR武器《咸鱼大剑--大剑》");
}else if (zb==4){
System.out.println("恭喜您掉落SSSR武器《北风-枪》");
}else if (zb==5){
System.out.println("恭喜您掉落SSSR武器《夜之阿莫斯--弓》");
}else if (zb==99){
System.out.println("恭喜您掉落绝版武器《妈妈的竹竿--千机伞》");
}else {
System.out.println("恭喜您获得20金币");
}
}
/*
* 描述自己的属性
* */
public void say(){
System.out.println("我叫"+name+",是一个"+type+",生命值高达"+life+",防御值"+defence+",攻击力"+attack+",暴击率"+crit());
}
public void pk(Player p) {
//定义一个标记,0为我方,1敌方攻击
int flag = 0;//默认我方先攻击
//提示当前战斗人员的信息
this.say();
p.say();
while (true) {
if (flag == 0) {
//战斗 我方攻击力-敌方防御力=伤害值
int harm = p.attack - this.defence;
//暴击
if (crit >= chance()){
this.life -= (2*harm);
System.out.println("现在轮到"+p.name+"攻击,(触发了暴击)"+this.name+"掉血"+(2*harm)+","+this.name+"剩余血量为"+this.life);
System.out.println();
}else {
this.life -= harm;//让敌方生命值-伤害值
//当前战斗
System.out.println("现在轮到"+p.name+"攻击,"+this.name+"掉血"+harm+","+this.name+"剩余血量为"+this.life);
System.out.println();
}
flag = 1;//改变标记,转换攻击角色
} else {
//战斗 敌方攻击力-我方防御力=伤害值
int harm = this.attack - p.defence;
if (crit >= chance()){
p.life -= (2*harm);
System.out.println("现在轮到"+this.name+"攻击,(触发了暴击)"+p.name+"掉血"+(2*harm)+","+p.name+"剩余血量为"+p.life);
System.out.println();
}else {
p.life -= harm;//让我方生命值-伤害值
//当前战斗
System.out.println("现在轮到"+this.name+"攻击,"+p.name+"掉血"+harm+","+p.name+"剩余血量为"+p.life);
System.out.println();
}
flag = 0;
}
//有血量<=0,战斗结束
if (this.life<=0){
System.out.println(this.name+"打败了"+p.name);
zb();
break;
}
if (p.life<=0){
System.out.println(p.name+"打败了"+this.name);
zb();
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 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;
}
//构造器:如果定义了一个有参的构造器,一定要构造无参构造器
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 Player() {
}
}
实现类如下:
package cn.hp;
/**
* 创建玩家进行对战
*/
public class PlayTest {
public static void main(String[] args) {
Player p1 = new Player("王日天","狂战士",1500,80,320);
Player p2 = new Player("赵穹弯","法师",1000,40,450);
p2.pk(p1);
}
}
边栏推荐
- Switch and Router Technology-31-Extended ACL
- 3 Module 2: Use of scientific research tools
- Four functional interfaces
- Application layer protocol - DNS
- Idea essential skills to improve work efficiency
- 关于CC 攻击
- MFC 进程间通信(共享内存)
- Optimization is a kind of habit low starting point is the "standing near the critical"
- Delphi7学习记录-demo实例
- Redis-使用jedis连接linux中redis服务器失败的解决方案
猜你喜欢
网络协议1
MySQL必知必会(初级篇)
C语言题解:谁是凶手!
Switch and Router Technology-33-Static NAT
Switch and Router Technology - 25 - OSPF Multi-Area Configuration
form form submission database Chinese becomes a question mark
什么是三次握手和四次挥手(清晰易懂)
redis连接idea
Redis-数据类型(基本指令、String、List、Set、Hash、ZSet、BitMaps、HyperLogLog、GeoSpatial)/发布和订阅
redis分布式锁
随机推荐
[QNX Hypervisor 2.2 User Manual] 10.16 vdev virtio-blk
IDEA中配置checkstyle
Delphi7 learning record - demo example
Tips to make your code more and more taller and taller - code specification, you have to know
redis连接idea
2022年质量员-土建方向-通用基础(质量员)考试模拟100题及在线模拟考试
2022年Android面试中最常问的问题是什么?
FPGA engineer interview questions collection 111~120
MySQL事务的概念
[Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
【无2022上海市安全员A证考试题库及模拟考试
Prometheus :(一)基本概念
Mysql入门练习
开发工具篇第七讲:阿里云日志查询与分析
[FPGA tutorial case 50] Control case 2 - FPGA-based PD controller verilog implementation
oracle表空间与用户的创建
Golden Warehouse Database KingbaseGIS User Manual (6.8. Geometry Object Input Function)
Project Practice Lecture 27: Application of Status Mode in Duplicate Brands
报表控件Stimulsoft报告中的数据矩阵条形码介绍
实战noVNC全过程操作(包含遇到的问题和解决)