java王者荣耀技能对战代码
时间: 2023-07-03 19:07:28 浏览: 73
以下是一个简单的 Java 王者荣耀技能对战代码示例:
```java
import java.util.Scanner;
public class SkillBattle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到王者荣耀技能对战!");
System.out.println("请输入英雄1的名称:");
String hero1 = scanner.nextLine();
System.out.println("请输入英雄2的名称:");
String hero2 = scanner.nextLine();
System.out.println(hero1 + " VS " + hero2 + ",开始对战!");
int hero1Hp = 100;
int hero2Hp = 100;
int round = 1;
while (hero1Hp > 0 && hero2Hp > 0) {
System.out.println("第" + round + "回合");
System.out.println(hero1 + "剩余血量:" + hero1Hp);
System.out.println(hero2 + "剩余血量:" + hero2Hp);
System.out.println("请选择技能(输入1或2):");
System.out.println("1.普通攻击");
System.out.println("2.技能攻击");
int skillChoice = scanner.nextInt();
int damage;
if (skillChoice == 1) {
damage = 10;
System.out.println("你使用了普通攻击!");
} else {
damage = 20;
System.out.println("你使用了技能攻击!");
}
int attackOrder = (int) (Math.random() * 2) + 1;
if (attackOrder == 1) {
System.out.println(hero1 + "先攻击!");
hero2Hp -= damage;
System.out.println(hero1 + "对" + hero2 + "造成了" + damage + "点伤害!");
if (hero2Hp <= 0) {
System.out.println(hero2 + "被击败了!");
break;
}
hero1Hp -= damage;
System.out.println(hero2 + "对" + hero1 + "造成了" + damage + "点伤害!");
if (hero1Hp <= 0) {
System.out.println(hero1 + "被击败了!");
break;
}
} else {
System.out.println(hero2 + "先攻击!");
hero1Hp -= damage;
System.out.println(hero2 + "对" + hero1 + "造成了" + damage + "点伤害!");
if (hero1Hp <= 0) {
System.out.println(hero1 + "被击败了!");
break;
}
hero2Hp -= damage;
System.out.println(hero1 + "对" + hero2 + "造成了" + damage + "点伤害!");
if (hero2Hp <= 0) {
System.out.println(hero2 + "被击败了!");
break;
}
}
round++;
}
System.out.println("游戏结束!");
}
}
```
这个代码示例实现了一个简单的王者荣耀技能对战游戏,其中包括两个英雄的名称、血量和技能攻击,每回合随机确定攻击顺序。你可以根据自己的需要进行修改和扩展。
阅读全文