当前位置:网站首页>课堂练习--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);
}
}
边栏推荐
- MySQL数据库管理
- C语句:数据存储
- [FPGA tutorial case 49] Control case 1 - FPGA-based PID controller verilog implementation
- Switch and Router Technology-31-Extended ACL
- 面试题整理
- Switch and Router Technology - 32 - Named ACL
- Decryption of BitLocker
- Some common mysql entry exercises
- K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster
- [Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
猜你喜欢

Prometheus :(一)基本概念

Switch and Router Technology-27-OSPF Route Redistribution

redis集群模式--解决redis单点故障

HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展

How IP-Guard prohibits running U disk programs

redis连接idea

C Language: Practical Debugging Tips

Switches and routers technologies - 30 - standard acls

Switch and Router Technology - 28 - NSSA Areas for OSPF

redis分布式锁
随机推荐
Switches and routers technology - 26 - configure OSPF peripheral area
Switches and routers technologies - 30 - standard acls
Idea 2021.3.3版本文件目录展开
C Language: Practical Debugging Tips
Thymeleaf
Zabbix builds enterprise-level monitoring and alarm platform
元宇宙社交应用,靠什么吸引用户「为爱发电」?
提升你工作效率的技巧,你得知道——Navitcat 快捷键
leetcode 9. Palindromic Numbers
How to read a paper
关于CC 攻击
StarUML使用心得
Switches and routers technology - 21 - RIP routing protocol
BitLocker的解密
MFC Interprocess Communication (Shared Memory)
In the closing pages/uninstall (unload) sends a request to the server before the document
nodes服务器
ARM Architecture 4: Embedded Hardware Platform Interface Development
Let's talk programming languages together
2021 Network Planning Designer Afternoon Case Questions