帮我做一个2个人对决的java 程序
时间: 2023-04-05 14:04:01 浏览: 103
java猜拳小游戏
好的,我可以回答这个问题。以下是一个简单的2个人对决的Java程序:
import java.util.Scanner;
public class Duel {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个人的名字:");
String player1 = input.nextLine();
System.out.print("请输入第二个人的名字:");
String player2 = input.nextLine();
int player1Health = 100;
int player2Health = 100;
while (player1Health > 0 && player2Health > 0) {
System.out.println(player1 + "的血量:" + player1Health);
System.out.println(player2 + "的血量:" + player2Health);
System.out.print(player1 + "攻击" + player2 + ",输入1:");
input.nextLine();
int damage1 = (int) (Math.random() * 10 + 1);
player2Health -= damage1;
System.out.println(player1 + "对" + player2 + "造成了" + damage1 + "点伤害!");
if (player2Health <= 0) {
System.out.println(player1 + "获胜!");
break;
}
System.out.print(player2 + "攻击" + player1 + ",输入2:");
input.nextLine();
int damage2 = (int) (Math.random() * 10 + 1);
player1Health -= damage2;
System.out.println(player2 + "对" + player1 + "造成了" + damage2 + "点伤害!");
if (player1Health <= 0) {
System.out.println(player2 + "获胜!");
break;
}
}
}
}
请注意,这只是一个简单的程序,仅供参考。如果您需要更复杂的程序,可能需要更多的代码和算法。
阅读全文