鱼C论坛

 找回密码
 立即注册
查看: 805|回复: 4

[已解决]输入缓冲区问题

[复制链接]
发表于 2025-3-19 19:59:14 | 显示全部楼层    本楼为最佳答案   
1. Animal.java

  1. public class Animal {
  2.     private String name;
  3.     private String species;
  4.     private int age;
  5.    
  6.     public Animal(String name, String species, int age) {
  7.         this.name = name;
  8.         this.species = species;
  9.         this.age = age;
  10.     }
  11.    
  12.     // Getters and setters
  13.     public String getName() {
  14.         return name;
  15.     }
  16.    
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.    
  21.     public String getSpecies() {
  22.         return species;
  23.     }
  24.    
  25.     public void setSpecies(String species) {
  26.         this.species = species;
  27.     }
  28.    
  29.     public int getAge() {
  30.         return age;
  31.     }
  32.    
  33.     public void setAge(int age) {
  34.         this.age = age;
  35.     }
  36.    
  37.     // Display animal information
  38.     public void display() {
  39.         System.out.println("- " + name + " (" + species + ", " + age + " years old)");
  40.     }
  41. }
复制代码


2. Item.java

  1. public class Item {
  2.     private String name;
  3.     private String code;
  4.     private double price;
  5.    
  6.     public Item(String name, String code) {
  7.         this.name = name;
  8.         this.code = code;
  9.         this.price = 0.0;
  10.     }
  11.    
  12.     // Getters and setters
  13.     public String getName() {
  14.         return name;
  15.     }
  16.    
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.    
  21.     public String getCode() {
  22.         return code;
  23.     }
  24.    
  25.     public void setCode(String code) {
  26.         this.code = code;
  27.     }
  28.    
  29.     public double getPrice() {
  30.         return price;
  31.     }
  32.    
  33.     public void setPrice(double price) {
  34.         this.price = price;
  35.     }
  36. }
复制代码


3. Logistics.java

  1. public class Logistics {
  2.     private Item vehicle;
  3.     private Item fuel;
  4.     private String[] caretakers;
  5.    
  6.     public Logistics(Item vehicle, Item fuel, String[] caretakers) {
  7.         this.vehicle = vehicle;
  8.         this.fuel = fuel;
  9.         this.caretakers = caretakers;
  10.     }
  11.    
  12.     // Display logistics information
  13.     public void display(String sourceZoo, String destinationZoo, Animal animal) {
  14.         System.out.println("\n----- LOGISTICS INFORMATION -----");
  15.         System.out.println("Moving: " + animal.getName() + " (" + animal.getSpecies() + ")");
  16.         System.out.println("From: " + sourceZoo);
  17.         System.out.println("To: " + destinationZoo);
  18.         System.out.println("Vehicle: " + vehicle.getName() + " (Code: " + vehicle.getCode() + ", Cost: RMB" + vehicle.getPrice() + ")");
  19.         System.out.println("Fuel: " + fuel.getName() + " (Code: " + fuel.getCode() + ", Cost: RMB" + fuel.getPrice() + ")");
  20.         
  21.         System.out.println("Caretakers:");
  22.         for (int i = 0; i < caretakers.length; i++) {
  23.             System.out.println("  " + (i+1) + ". " + caretakers[i]);
  24.         }
  25.         
  26.         double totalCost = vehicle.getPrice() + fuel.getPrice();
  27.         System.out.println("Total Transportation Cost: RMB" + totalCost);
  28.         System.out.println("----------------------------------");
  29.     }
  30. }
复制代码


4. Zoo.java

  1. public class Zoo {
  2.     private String name;
  3.     private Animal[] animals;
  4.     private int counter;

  5.     public Zoo(String name) {
  6.         this.name = name;
  7.         this.animals = new Animal[10];
  8.         this.counter = 0;
  9.     }

  10.     // Set and get name
  11.     public void setName(String name) {
  12.         this.name = name;
  13.     }

  14.     public String getName() {
  15.         return name;
  16.     }

  17.     // Set and get counter
  18.     public void setCounter(int counter) {
  19.         this.counter = counter;
  20.     }

  21.     public int getCounter() {
  22.         return counter;
  23.     }

  24.     // Add an animal to the zoo
  25.     public boolean addAnimal(Animal animal) {
  26.         if (counter < animals.length) {
  27.             animals[counter++] = animal;
  28.             return true;
  29.         } else {
  30.             System.out.println(name + " is full. Cannot add more animals.");
  31.             return false;
  32.         }
  33.     }

  34.     // Delete an animal from the zoo
  35.     public void deleteAnimal(String name) {
  36.         for (int i = 0; i < counter; i++) {
  37.             if (animals[i].getName().equals(name)) {
  38.                 for (int j = i; j < counter - 1; j++) {
  39.                     animals[j] = animals[j + 1];
  40.                 }
  41.                 animals[--counter] = null;
  42.                 System.out.println("Animal '" + name + "' removed from " + this.name);
  43.                 return;
  44.             }
  45.         }
  46.         System.out.println("Animal '" + name + "' not found.");
  47.     }

  48.     public int findAnimal(String name) {
  49.         for (int i = 0; i < counter; i++) {
  50.             if (animals[i].getName().equals(name)) {
  51.                 return i; // Return the index if the animal is found
  52.             }
  53.         }
  54.         return -1; // Return -1 if the animal is not found
  55.     }

  56.     // Get animal by index
  57.     public Animal getAnimal(int index) {
  58.         if (index >= 0 && index < counter) {
  59.             return animals[index];
  60.         }
  61.         return null;
  62.     }

  63.     // Move an animal to another zoo
  64.     public void moveAnimal(String name, Zoo destination, Logistics logistics) {
  65.         int index = findAnimal(name);
  66.         if (index != -1) {
  67.             Animal animal = animals[index];
  68.             boolean added = destination.addAnimal(animal);
  69.             if (added) {
  70.                 logistics.display(this.name, destination.getName(), animal);
  71.                 System.out.println("Animal '" + name + "' added to " + destination.getName());
  72.                 System.out.println("Animal '" + name + "' successfully moved from " + this.name + " to " + destination.getName());
  73.                 deleteAnimal(name);
  74.             }
  75.         } else {
  76.             System.out.println("Animal '" + name + "' not found.");
  77.         }
  78.     }

  79.     // Display all animals
  80.     public void displayAnimals() {
  81.         System.out.println("\n--- Animals in " + name + " (" + counter + "/10) ---");
  82.         if (counter == 0) {
  83.             System.out.println("No animals in this zoo.");
  84.         } else {
  85.             for (int i = 0; i < counter; i++) {
  86.                 animals[i].display();
  87.             }
  88.         }
  89.         System.out.println("---------------------------------------\n");
  90.     }
  91. }
复制代码


5. ZooManagementSystem.java

  1. import java.util.Scanner;

  2. public class ZooManagementSystem {
  3.     public static void main(String[] args) {
  4.         // 创建用户输入的scanner
  5.         Scanner input = new Scanner(System.in);
  6.         
  7.         // 创建两个动物园实例
  8.         Zoo southernZone = new Zoo("Southern-Zone Zoo");
  9.         Zoo northernZone = new Zoo("Northern-Zone Zoo");
  10.         
  11.         // 向Southern-Zone Zoo添加5只动物
  12.         southernZone.addAnimal(new Animal("Simba", "African Lion", 6));
  13.         southernZone.addAnimal(new Animal("Dumbo", "African Elephant", 12));
  14.         southernZone.addAnimal(new Animal("Luna", "Gray Wolf", 4));
  15.         southernZone.addAnimal(new Animal("Poe", "Raven", 3));
  16.         southernZone.addAnimal(new Animal("Benny", "Grizzly Bear", 8));
  17.         
  18.         // 向Northern-Zone Zoo添加5只动物
  19.         northernZone.addAnimal(new Animal("Arctic", "Polar Bear", 7));
  20.         northernZone.addAnimal(new Animal("Blizzard", "Snow Leopard", 5));
  21.         northernZone.addAnimal(new Animal("Frost", "Arctic Fox", 3));
  22.         northernZone.addAnimal(new Animal("Penguin", "Emperor Penguin", 4));
  23.         northernZone.addAnimal(new Animal("Aurora", "Caribou", 6));
  24.         
  25.         // 显示两个动物园中的所有动物
  26.         southernZone.displayAnimals();
  27.         northernZone.displayAnimals();
  28.         
  29.         // 动物园管理交互菜单
  30.         boolean running = true;
  31.         while (running) {
  32.             // 显示菜单选项
  33.             System.out.println("\n===== ZOO MANAGEMENT SYSTEM =====");
  34.             System.out.println("1. Display all animals in Southern-Zone Zoo");
  35.             System.out.println("2. Display all animals in Northern-Zone Zoo");
  36.             System.out.println("3. Move an animal between zoos");
  37.             System.out.println("4. Add a new animal to a zoo");
  38.             System.out.println("5. Remove an animal from a zoo");
  39.             System.out.println("6. Find an animal");
  40.             System.out.println("0. Exit");
  41.             System.out.print("Enter your choice: ");
  42.             
  43.             // 获取用户选择
  44.             int choice = input.nextInt();
  45.             input.nextLine(); // 清除缓冲区
  46.             
  47.             // 处理用户选择
  48.             switch (choice) {
  49.                 case 1:
  50.                     southernZone.displayAnimals();
  51.                     break;
  52.                     
  53.                 case 2:
  54.                     northernZone.displayAnimals();
  55.                     break;
  56.                     
  57.                 case 3:
  58.                     // 在动物园之间移动动物
  59.                     System.out.println("\n----- MOVE ANIMAL BETWEEN ZOOS -----");
  60.                     System.out.println("1. Move from Southern-Zone to Northern-Zone");
  61.                     System.out.println("2. Move from Northern-Zone to Southern-Zone");
  62.                     System.out.print("Enter your choice: ");
  63.                     
  64.                     // 根据用户选择确定源和目标动物园
  65.                     int moveChoice = input.nextInt();
  66.                     input.nextLine(); // 清除缓冲区
  67.                     
  68.                     Zoo from = (moveChoice == 1) ? southernZone : northernZone;
  69.                     Zoo to = (moveChoice == 1) ? northernZone : southernZone;
  70.                     
  71.                     // 显示源动物园中可用的动物
  72.                     System.out.println("Available animals in " + from.getName() + ":");
  73.                     from.displayAnimals();
  74.                     
  75.                     // 检查源动物园是否有动物可移动
  76.                     if (from.getCounter() == 0) {
  77.                         System.out.println("No animals available to move.");
  78.                         break;
  79.                     }
  80.                     
  81.                     // 获取要移动的动物名称
  82.                     System.out.print("Enter the name of the animal to move: ");
  83.                     String animalName = input.nextLine();
  84.                     
  85.                     // 检查动物是否存在于源动物园
  86.                     int index = from.findAnimal(animalName);
  87.                     if (index != -1) {
  88.                         // 设置移动的物流
  89.                         System.out.println("\nSetting up logistics for the move...");
  90.                         
  91.                         // 创建车辆项目
  92.                         Item vehicle = new Item("Transport Truck", "VEH_001");
  93.                         System.out.print("Enter vehicle cost: RMB");
  94.                         vehicle.setPrice(input.nextDouble());
  95.                         input.nextLine(); // 清除缓冲区
  96.                         
  97.                         // 创建燃料项目
  98.                         Item fuel = new Item("Diesel Fuel", "FUEL_001");
  99.                         System.out.print("Enter fuel cost: RMB");
  100.                         fuel.setPrice(input.nextDouble());
  101.                         input.nextLine(); // 清除缓冲区
  102.                         
  103.                         // 设置照料者
  104.                         int numCaretakers;
  105.                         // 确保数字在1-3之间
  106.                         do {
  107.                             System.out.print("Enter the number of caretakers (between 1 and 3): ");
  108.                             numCaretakers = input.nextInt();
  109.                             input.nextLine(); // 清除缓冲区

  110.                             if (numCaretakers < 1 || numCaretakers > 3) {
  111.                                 System.out.println("Invalid input! Please enter a number between 1 and 3.");
  112.                             }
  113.                         } while (numCaretakers < 1 || numCaretakers > 3);

  114.                         System.out.println("Number of caretakers set to: " + numCaretakers);
  115.                         
  116.                         // 获取照料者姓名
  117.                         String[] caretakers = new String[numCaretakers];
  118.                         for (int i = 0; i < numCaretakers; i++) {
  119.                             System.out.print("Enter name of caretaker " + (i+1) + ": ");
  120.                             caretakers[i] = input.nextLine();
  121.                         }
  122.                         
  123.                         // 创建带有所有组件的物流对象
  124.                         Logistics logistics = new Logistics(vehicle, fuel, caretakers);
  125.                         
  126.                         // 将动物从源动物园移动到目标动物园
  127.                         from.moveAnimal(animalName, to, logistics);
  128.                         
  129.                         // 显示更新后的动物园信息
  130.                         from.displayAnimals();
  131.                         to.displayAnimals();
  132.                     } else {
  133.                         System.out.println("--- Animal '" + animalName + "' not found in " + from.getName());
  134.                     }
  135.                     break;
  136.                     
  137.                 case 4:
  138.                     // 添加新动物
  139.                     System.out.println("\n----- ADD NEW ANIMAL -----");
  140.                     System.out.println("1. Add to Southern-Zone Zoo");
  141.                     System.out.println("2. Add to Northern-Zone Zoo");
  142.                     System.out.print("Enter your choice: ");
  143.                     
  144.                     // 确定要添加到哪个动物园
  145.                     int addChoice = input.nextInt();
  146.                     input.nextLine(); // 清除缓冲区
  147.                     
  148.                     Zoo selectedZooForAdd = (addChoice == 1) ? southernZone : northernZone;
  149.                     
  150.                     // 从用户获取动物详细信息
  151.                     System.out.print("Enter animal name: ");
  152.                     String name = input.nextLine();
  153.                     
  154.                     System.out.print("Enter animal species: ");
  155.                     String species = input.nextLine();
  156.                     
  157.                     System.out.print("Enter animal age: ");
  158.                     int age = input.nextInt();
  159.                     input.nextLine(); // 清除缓冲区
  160.                     
  161.                     // 创建并添加新动物
  162.                     Animal newAnimal = new Animal(name, species, age);
  163.                     boolean added = selectedZooForAdd.addAnimal(newAnimal);
  164.                     
  165.                     if (added) {
  166.                         System.out.println("Animal added successfully!");
  167.                     }
  168.                     
  169.                     // 显示更新后的动物园
  170.                     selectedZooForAdd.displayAnimals();
  171.                     break;
  172.                     
  173.                 case 5:
  174.                     // 移除动物
  175.                     System.out.println("\n----- REMOVE ANIMAL -----");
  176.                     System.out.println("1. Remove from Southern-Zone Zoo");
  177.                     System.out.println("2. Remove from Northern-Zone Zoo");
  178.                     System.out.print("Enter your choice: ");
  179.                     
  180.                     // 确定从哪个动物园移除
  181.                     int removeChoice = input.nextInt();
  182.                     input.nextLine(); // 清除缓冲区
  183.                     
  184.                     Zoo selectedZooForRemove = (removeChoice == 1) ? southernZone : northernZone;
  185.                     
  186.                     // 显示可用的动物
  187.                     selectedZooForRemove.displayAnimals();
  188.                     
  189.                     // 检查动物园是否有动物可移除
  190.                     if (selectedZooForRemove.getCounter() == 0) {
  191.                         System.out.println("No animals to remove.");
  192.                         break;
  193.                     }
  194.                     
  195.                     // 获取要移除的动物名称
  196.                     System.out.print("Enter the name of the animal to remove: ");
  197.                     String animalToRemove = input.nextLine();
  198.                     
  199.                     // 移除动物并显示更新后的动物园
  200.                     selectedZooForRemove.deleteAnimal(animalToRemove);
  201.                     selectedZooForRemove.displayAnimals();
  202.                     break;
  203.                     
  204.                 case 6:
  205.                     // 查找动物
  206.                     System.out.println("\n----- FIND ANIMAL -----");
  207.                     System.out.print("Enter the name of the animal to find: ");
  208.                     String animalToFind = input.nextLine();
  209.                     
  210.                     // 在两个动物园中搜索动物
  211.                     int indexSouthern = southernZone.findAnimal(animalToFind);
  212.                     int indexNorthern = northernZone.findAnimal(animalToFind);
  213.                     
  214.                     // 根据动物被发现的位置显示结果
  215.                     if (indexSouthern != -1) {
  216.                         System.out.println("Animal found in Southern-Zone Zoo:");
  217.                         southernZone.getAnimal(indexSouthern).display();
  218.                     } else if (indexNorthern != -1) {
  219.                         System.out.println("Animal found in Northern-Zone Zoo:");
  220.                         northernZone.getAnimal(indexNorthern).display();
  221.                     } else {
  222.                         System.out.println("Animal '" + animalToFind + "' not found in either zoo.");
  223.                     }
  224.                     break;
  225.                     
  226.                 case 0:
  227.                     running = false;
  228.                     System.out.println("-----*****Exiting Zoo Management System. Goodbye!******-----");
  229.                     break;
  230.                     
  231.                 default:
  232.                     System.out.println("Invalid choice. Please try again.");
  233.             }
  234.         }
  235.         
  236.         // 关闭scanner
  237.         input.close();
  238.     }
  239. }
复制代码


主要修复内容:

  • 创建了缺失的Animal、Item和Logistics类
  • 修复了ZooManagementSystem中的输入处理问题,确保在使用nextInt()后调用nextLine()清除缓冲区
  • 增加了更详细的注释(中文)
  • 确保在程序退出时关闭Scanner

这个实现现在应该可以正常工作,处理了之前代码中的所有问题。

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-22 03:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表