当前位置:网站首页>课堂练习--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);
}
}
边栏推荐
- ARM Architecture 4: Embedded Hardware Platform Interface Development
- MySQL must know and must know (primary articles)
- Sub-database sub-table ShardingSphere-JDBC notes arrangement
- shell 脚本编程---入门
- BGP综合实验
- 每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
- prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
- HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
- 金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
- redis集群模式--解决redis单点故障
猜你喜欢

宝塔Linux环境下redis开启多端口

Switches and routers technologies - 30 - standard acls

每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
![ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx](/img/02/3896b29a955ae84a0f0326f0d2cabf.png)
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx

Redis详解

一个月闭关直接面进大厂,这份Android面试笔记是真的牛逼

C statement: data storage

Idea提升工作效率的必备技巧

BGP综合实验

Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
随机推荐
You must understand - the nine built-in objects and four domain objects of JSP
2022 building welder (building a special type of work) examination questions and simulation test
提升你工作效率的技巧,你得知道——Navitcat 快捷键
Switch and Router Technology-35-NAT to PAT
form form submission database Chinese becomes a question mark
Switches and routers technologies - 30 - standard acls
[No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
Mysql introductory exercise
Switch and Router Technology - 25 - OSPF Multi-Area Configuration
Kong实现禁止国外IP访问
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster
Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
How IP-Guard prohibits running U disk programs
Day38 LeetCode
Redis详解
FPGA engineer interview questions collection 121~130
BitLocker的解密
Zabbix builds enterprise-level monitoring and alarm platform
[E-commerce operation] How to formulate a social media marketing strategy?
Switch and Router Technology-29-OSPF Virtual Link