使用Java编写一个养宠物的程序
时间: 2024-03-06 11:48:57 浏览: 69
好的,我可以给您提供一个简单的使用Java编写的养宠物程序的例子,可以实现添加宠物、喂食、玩耍、查看宠物信息等功能。
首先,我们需要定义一个宠物类Pet,包含宠物的属性和方法:
```
public class Pet {
private String name; // 宠物名字
private int age; // 宠物年龄
private int health; // 宠物健康值
private int hunger; // 宠物饥饿值
public Pet(String name, int age) {
this.name = name;
this.age = age;
this.health = 100;
this.hunger = 0;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getHealth() {
return health;
}
public int getHunger() {
return hunger;
}
public void feed() {
hunger -= 20;
if (hunger < 0) {
hunger = 0;
}
health += 10;
if (health > 100) {
health = 100;
}
}
public void play() {
hunger += 10;
if (hunger > 100) {
hunger = 100;
}
health -= 20;
if (health < 0) {
health = 0;
}
}
public String toString() {
return "Pet[name=" + name + ",age=" + age + ",health=" + health + ",hunger=" + hunger + "]";
}
}
```
然后,我们需要定义一个宠物管理器类PetManager,包含添加宠物、喂食、玩耍、查看宠物信息等方法:
```
import java.util.ArrayList;
public class PetManager {
private ArrayList<Pet> pets; // 存储宠物的列表
public PetManager() {
pets = new ArrayList<Pet>();
}
public void addPet(Pet pet) {
pets.add(pet);
}
public void feedPet(int index) {
pets.get(index).feed();
}
public void playWithPet(int index) {
pets.get(index).play();
}
public Pet getPet(int index) {
return pets.get(index);
}
public int getPetCount() {
return pets.size();
}
}
```
最后,我们需要定义一个用户界面类PetUI,用于与用户交互,例如显示宠物信息、提供喂食、玩耍等操作按钮:
```
import java.util.Scanner;
public class PetUI {
private PetManager petManager;
public PetUI() {
petManager = new PetManager();
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. 添加宠物");
System.out.println("2. 喂食");
System.out.println("3. 玩耍");
System.out.println("4. 查看宠物信息");
System.out.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addPet();
break;
case 2:
feedPet();
break;
case 3:
playWithPet();
break;
case 4:
showPetInfo();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入");
break;
}
}
}
private void addPet() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入宠物名字:");
String name = scanner.nextLine();
System.out.println("请输入宠物年龄:");
int age = scanner.nextInt();
Pet pet = new Pet(name, age);
petManager.addPet(pet);
System.out.println("添加宠物成功");
}
private void feedPet() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要喂食的宠物:");
for (int i = 0; i < petManager.getPetCount(); i++) {
System.out.println((i + 1) + ". " + petManager.getPet(i).getName());
}
int choice = scanner.nextInt();
petManager.feedPet(choice - 1);
System.out.println("喂食成功");
}
private void playWithPet() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要玩耍的宠物:");
for (int i = 0; i < petManager.getPetCount(); i++) {
System.out.println((i + 1) + ". " + petManager.getPet(i).getName());
}
int choice = scanner.nextInt();
petManager.playWithPet(choice - 1);
System.out.println("玩耍成功");
}
private void showPetInfo() {
System.out.println("所有宠物信息:");
for (int i = 0; i < petManager.getPetCount(); i++) {
System.out.println(petManager.getPet(i));
}
}
}
```
最后,我们可以在main方法中启动用户界面:
```
public class Main {
public static void main(String[] args) {
PetUI ui = new PetUI();
ui.start();
}
}
```
这个程序只是一个简单的例子,您可以根据自己的需求来扩展和改进这个程序。
阅读全文