软件更新时间: 2024-11-02 09:11:50 / 版本:V3.09.24 / 大小:150MB
详情内容
在这个JAVA代码示例中,我们将演示如何创建一个简单的人狗大战游戏。游戏包括两个类:人类(Person)和狗类(Dog)。
```java // 人类 public class Person { private String name; private int health; public Person(String name, int health) { this.name = name; this.health = health; } public void attack(Dog dog) { dog.takeDamage(10); } public void takeDamage(int damage) { health -= damage; if (health <= 0) { System.out.println(name + "被击败了!"); } } } // 狗类 public class Dog { private String name; private int health; public Dog(String name, int health) { this.name = name; this.health = health; } public void attack(Person person) { person.takeDamage(5); } public void takeDamage(int damage) { health -= damage; if (health <= 0) { System.out.println(name + "被击败了!"); } } } // 游戏主类 public class Main { public static void main(String[] args) { Person person = new Person("玩家1", 100); Dog dog = new Dog("狗1", 50); while (person.health > 0 && dog.health > 0) { person.attack(dog); dog.attack(person); } } } ``` 这段代码展示了一个简单的人狗大战游戏,玩家和狗互相攻击直到其中一方被击败。同类内容