dajiayouxi.java
[Java] 纯文本查看 复制代码 package cn.itcast.day04.xiaoyouxi;
import java.util.Scanner;
/*
打架小游戏
*/
public class dajiayouxi {
//计数器,记录回合数
static int count=0;
//键盘录入,用以人机交互
static Scanner sc=new Scanner(System.in);
//主方法,定义属性,调用循环
public static void main(String[] args) {
//生成路人甲角色的随机属性
Generate lurenjia=new Generate();lurenjia.g("路人甲");
//生成路人乙角色的随机属性
Generate lurenyi=new Generate();lurenyi.g("路人乙");
//确认战斗开始
{
System.out.println("请选择角色(偶数为路人甲,奇数为路人乙):");
int ii=sc.nextInt();
if (ii%2==0){
System.out.println("--------------你选择的是路人甲,你的对手是路人乙--------------");
}else{
System.out.println("--------------你选择的是路人乙,你的对手是路人甲--------------");
}
System.out.println("输入任意字符进入对战");
String ss=sc.next();
}
//第一部分属性展示
zs(lurenjia,lurenyi);
//输入任意字符下一回合直至一方死亡结束回合
{
while (true){
System.out.println("输入任意字符下一回合");
String sss=sc.next();
if (printLifes(lurenjia,lurenyi)){
}else {
break;
}
}
}
}
//输出每回合状态
public static boolean printLifes(Generate lurenjia,Generate lurenyi){
System.out.println("----------------------VS----------------------");
System.out.println(" ****************************");
//双方是否触发增益状态
ZKBG.print(lurenjia);
ZKBG.print(lurenyi);
//双方扣除的血量
lurenjia.setLife(lurenjia.getLife()-lurenyi.getAttack()+lurenjia.getRecover());
System.out.println(" 路人甲-"+lurenyi.getAttack());
lurenyi.setLife(lurenyi.getLife()-lurenjia.getAttack()+lurenyi.getRecover());
System.out.println(" 路人乙-"+lurenjia.getAttack());
System.out.println(" ****************************\n");
//回合数每次加一
count++;
System.out.println("路人甲");
printLife(lurenjia.getLife());
System.out.println("攻击力:" + lurenjia.getAttack());
System.out.println("每回合血量+" + lurenjia.getRecover());
System.out.println("-----第"+count+"回合-----");
System.out.println("路人乙");
printLife(lurenyi.getLife());
System.out.println("攻击力:" + lurenyi.getAttack());
System.out.println("每回合血量+" + lurenyi.getRecover());
System.out.println("----------------------------------------------");
return pd(lurenjia,lurenyi);
}
//判断是否结束战斗
public static boolean pd(Generate lurenjia,Generate lurenyi){
if ((lurenjia.getLife()<=0&&lurenyi.getLife()<=0)||count>15){
System.out.println("同归于尽");
return false;
}else if(lurenjia.getLife()<=0){
System.out.println("路人甲输了");
lurenjia.setLife(0);
return false;
}else if(lurenyi.getLife()<=0){
System.out.println("路人乙输了");
lurenyi.setLife(0);
return false;
}
return true;
}
//判断生命值是否为负数
public static void printLife(int x){
if (x<=0){
System.out.println("生命值:0");
}else {
System.out.println("生命值:"+x);
}
}
//第一部分属性展示
public static void zs(Generate lurenjia,Generate lurenyi){
System.out.println("----------------------VS----------------------");
System.out.println("路人甲");
printLife(lurenjia.getLife());
System.out.println("攻击力:" + lurenjia.getAttack());
System.out.println("每回合血量+" + lurenjia.getRecover());
System.out.println("------对阵------");
System.out.println("路人乙");
printLife(lurenyi.getLife());
System.out.println("攻击力:" + lurenyi.getAttack());
System.out.println("每回合血量+" + lurenyi.getRecover());
}
}
ZKBG.java
[Java] 纯文本查看 复制代码 package cn.itcast.day04.xiaoyouxi;
import java.util.Random;
public class ZKBG {
public ZKBG() {
}
public static void print(Generate s){
Random r1=new Random();
int zt=r1.nextInt(8)+1;
if (zt<=3&&zt>0){
if (s.getLife()<20){
s.setAttack(s.getAttack()*2);
System.out.println(s.getName()+"发动了狂暴!");
System.out.println(s.getName()+"攻击*2\n");
}
}else if (zt<=6&&zt>3){
s.setAttack(s.getAttack()+3);
System.out.println(s.getName()+"发动了斗志!");
System.out.println(s.getName()+"攻击+3\n");
}else {
s.setLife(s.getLife()+10);
System.out.println(s.getName()+"使用了绷带!");
System.out.println(s.getName()+"+10\n");
}
}
}
Generate.java
[Java] 纯文本查看 复制代码 package cn.itcast.day04.xiaoyouxi;
import java.util.Random;
public class Generate {
private int attack=0;
private int life=0;
private int recover=0;
private String name;
public Generate() {
}
public Generate(int attack, int life, int recover,String name) {
this.attack = attack;
this.life = life;
this.recover = recover;
this.name=name;
}
public void g(String s){
while (true){
Random r2=new Random();
this.name=s;
this.setAttack(r2.nextInt(9)+1);
this.setLife(r2.nextInt(45)+1);
this.setRecover(recover=r2.nextInt(6)+1);
int count= this.attack+this.life+this.recover;
if (count>=45) {
break;
}
}
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getRecover() {
return recover;
}
public void setRecover(int recover) {
this.recover = recover;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
|