【发布时间】:2021-08-14 04:13:39
【问题描述】:
大家晚上好,
我目前正在介绍 Java 类,但我似乎遇到了一些问题。本质上,该项目是创建一个系统,在该系统中接收救援动物、保留动物并打印动物名单。我遇到的问题是,当我摄入一只新动物时,程序会循环遍历所有问题,甚至将动物添加到数组中,但只是继续循环遍历问题。例如: 狗叫什么名字:Fido 你的狗是什么品种:实验室 你的狗几岁:3 狗叫什么名字:Fido 那条狗已经在我们的系统中了。请再试一次。 狗叫什么名字:雷克斯 你的狗是什么品种:实验室
以此类推,一直如此。我不确定我在哪里犯了错误。我在下面包含了我的代码。感谢您的帮助。
/**************************************************************
/*Program designed to input rescue animal information, reserve
/*rescue animals, and print lists of rescue animals.
/*Written by: Nicholas Phelps
/*Date: July 30, 2021
/**************************************************************/
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
private static ArrayList<Dog> dogList = new ArrayList<Dog>();
private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();
// Instance variables (if needed)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String userInput;
initializeDogList();
initializeMonkeyList();
/***********************************************************************************
* Do while menu loop that takes user input, validates, and executes menu option
* Updated the menu validation to .equals("userSelection") instead of "=="
**********************************************************************************/
displayMenu();
userInput = scanner.nextLine();
do {
if(userInput.equals("1")) {
intakeNewDog(scanner);
}
else if(userInput.equals("2")) {
intakeNewMonkey(scanner);
}
else if(userInput.equals("3")) {
reserveAnimal(scanner);
}
else if(userInput.equals("4")) {
printAnimals(scanner);
}
else if(userInput.equals("5")) {
printAnimals(scanner);
}
else if(userInput.equals("6")) {
printAnimals(scanner);
}
else {
System.out.println("Invalid choice. Please enter a valid option.");
}
}while(!userInput.equals("q"));
}
// This method prints the menu options
public static void displayMenu() {
System.out.println("\n\n");
System.out.println("\t\t\t\tRescue Animal System Menu");
System.out.println("[1] Intake a new dog");
System.out.println("[2] Intake a new monkey");
System.out.println("[3] Reserve an animal");
System.out.println("[4] Print a list of all dogs");
System.out.println("[5] Print a list of all monkeys");
System.out.println("[6] Print a list of all animals that are not reserved");
System.out.println("[q] Quit application");
System.out.println();
System.out.println("Enter a menu selection");
}
// Adds dogs to a list for testing
public static void initializeDogList() {
Dog dog1 = new Dog("Spot", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
Dog dog2 = new Dog("Rex", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States");
Dog dog3 = new Dog("Bella", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");
dogList.add(dog1);
dogList.add(dog2);
dogList.add(dog3);
}
// Adds monkeys to a list for testing
//Optional for testing
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey("Marcel", "Capuchin", 9, 18, 18, "male", "2", "14.7", "11-13-2019", "United States", "intake", false, "United States");
Monkey monkey2 = new Monkey("Ross", "Marmoset", 10, 19, 19, "male", "3", "15", "01-22-2018", "France", "in service", true, "France");
Monkey monkey3 = new Monkey("Remy", "Squirrel Monkey", 6, 12, 12, "female", "2", "8", "04-01-2019", "United States", "Phase I", false, "United States");
monkeyList.add(monkey1);
monkeyList.add(monkey2);
monkeyList.add(monkey3);
}
/*******************************************************************************************************
* Completed the intakeNewDog method that takes user input, validates that input and sets dog class
* variables. Then adds the new dog to the dogList array.
******************************************************************************************************/
public static void intakeNewDog(Scanner scanner) {
System.out.println("What is the dog's name?");
String name = scanner.nextLine();
for(Dog dog: dogList) {
if(dog.getName().equalsIgnoreCase(name)) {
System.out.println("\n\nThis dog is already in our system\n\n");
return; //returns to menu
}
else {
dog.setName(name);
System.out.println("What is the dog's breed?");
String breed = scanner.nextLine();
dog.setBreed(breed);
System.out.println("What is the dog's gender?");
String gender = scanner.nextLine();
dog.setGender(gender);
System.out.println("What is the dog's age?");
String age = scanner.nextLine();
dog.setAge(age);
System.out.println("What is the dog's weight?");
String weight = scanner.nextLine();
dog.setWeight(weight);
System.out.println("What is the dog's acquisition date?");
String acquisitionDate = scanner.nextLine();
dog.setAcquisitionDate(acquisitionDate);
System.out.println("What is the dog's acquisition country?");
String acquisitionCountry = scanner.nextLine();
dog.setAcquisitionLocation(acquisitionCountry);
System.out.println("What is the dog's training status?");
String trainingStatus = scanner.nextLine();
dog.setTrainingStatus(trainingStatus);
System.out.println("Is the dog reserved?");
boolean reserved = scanner.nextBoolean(); scanner.nextLine();
dog.setReserved(reserved);
System.out.println("What is the dog's service country?");
String inServiceCountry = scanner.nextLine();
dog.setInServiceCountry(inServiceCountry);
Dog dog4 = new Dog(name, breed, gender, age, weight, acquisitionDate,
acquisitionCountry, trainingStatus, reserved, inServiceCountry);
dogList.add(dog4);
}
return;
/**
* Loop never ends and continuously adds dogs. Need a way to break loop
*/
}
}
/***************************************************************************************
* Implemented intakeNewMonkey method that takes user input, validates that input, and
* sets monkey variables. Then adds the new monkey to monkeyList
**************************************************************************************/
public static void intakeNewMonkey(Scanner scanner) {
System.out.println("What is the monkey's name?");
String name = scanner.nextLine();
for(Monkey monkey: monkeyList) {
if(monkey.getName().equalsIgnoreCase(name)) {
System.out.println("\n\nThis monkey is already in our system\n\n");
return;
}
else {
monkey.setName(name);
System.out.println("What is the monkey's species?"); //Monkey Species
String species = scanner.nextLine();
monkey.setSpecies(species);
System.out.println("What is the monkey's tail length?"); //Monkey tail length
int tailLength = scanner.nextInt();
monkey.setTailLength(tailLength);
System.out.println("What is the monkey's height?"); //Monkey height
int height = scanner.nextInt();
monkey.setHeight(height);
System.out.println("What is the monkey's body lenght?"); //Monkey body length
int bodyLength = scanner.nextInt();
monkey.setBodyLength(bodyLength);
System.out.println("What is the monkey's gender?"); //Monkey gender
String gender = scanner.nextLine();
monkey.setGender(gender);
System.out.println("What is the monkey's age?"); //Monkey age
String age = scanner.nextLine();
monkey.setAge(age);
System.out.println("What is the monkey's weight?"); //Monkey weight
String weight = scanner.nextLine();
monkey.setWeight(weight);
System.out.println("What was the monkey's aquisition date?"); //Monkey acquisition date
String aquisitionDate = scanner.nextLine();
monkey.setAcquisitionDate(aquisitionDate);
System.out.println("What was the monkey's aquisition country?"); //Monkey acquisition country
String aquisitionCountry = scanner.nextLine();
monkey.setAcquisitionLocation(aquisitionCountry);
System.out.println("What is the monkey's training status?"); //Monkey training status
String trainingStatus = scanner.nextLine();
monkey.setTrainingStatus(trainingStatus);
System.out.println("Is the monkey reserved?"); //Monkey reservation status
boolean reserved = scanner.nextBoolean(); scanner.nextLine();
monkey.setReserved(reserved);
System.out.println("What is the monkey's service country?"); //Monkey service country
String inServiceCountry = scanner.nextLine();
monkey.setInServiceCountry(inServiceCountry);
Monkey monkey4 = new Monkey (name, species, tailLength, height, bodyLength, gender, age, weight, aquisitionDate,
aquisitionCountry, trainingStatus, reserved, inServiceCountry);
monkeyList.add(monkey4);
return;
}
}
}
/*******************************************************************************************
* Implemented the reserveAnimal method that takes user input for type of rescue animal,
* breed / species, and service country. Then validates that input to ensure that it is
* valid user input and that there is a service animal of the chosen species/breed in
* the selected service country available. If there is then the animal is reserved and if
* not then the system sends a message asking user to make a different selection.
*******************************************************************************************/
public static void reserveAnimal(Scanner scanner) {
System.out.println("Enter type of rescue animal (dog or monkey): ");
String rescueAnimal = scanner.nextLine();
if(rescueAnimal.equals("monkey")) {
System.out.println("Enter monkey species: ");
String species = scanner.nextLine();
System.out.println("Enter service country: ");
String inServiceCountry = scanner.nextLine();
for(Monkey monkey: monkeyList) {
if(monkey.getSpecies().equalsIgnoreCase(species) || monkey.getInServiceLocation().equalsIgnoreCase(inServiceCountry)) {
monkey.setReserved(true);
monkey.setInServiceCountry(inServiceCountry);
return;
}
else {
System.out.println("No rescue monkey of this species in selected service country.");
System.out.println("Please make another choice");
return;
}
}
}
else if(rescueAnimal.equalsIgnoreCase("dog")) {
System.out.println("Enter dog's breed: ");
String breed = scanner.nextLine();
System.out.println("Enter dog's service country: ");
String inServiceCountry = scanner.nextLine();
for(Dog dog: dogList) {
if(dog.getBreed().equalsIgnoreCase(breed) || dog.getInServiceLocation().equalsIgnoreCase(inServiceCountry)) {
dog.setReserved(true);
dog.setInServiceCountry(inServiceCountry);
return;
}
else {
System.out.println("No rescue dog of that breed in selected service country.");
System.out.println("Please make another choice.");
return;
}
}
}
else {
System.out.println("Invalid choice. Please make a valid choice.");
return;
}
}
// Complete printAnimals
// Include the animal name, status, acquisition country and if the animal is reserved.
// Remember that this method connects to three different menu items.
// The printAnimals() method has three different outputs
// based on the listType parameter
// dog - prints the list of dogs
// monkey - prints the list of monkeys
// available - prints a combined list of all animals that are
// fully trained ("in service") but not reserved
// Remember that you only have to fully implement ONE of these lists.
// The other lists can have a print statement saying "This option needs to be implemented".
// To score "exemplary" you must correctly implement the "available" list.
public static void printAnimals(Scanner scanner) {
String userInput;
System.out.println("[1] Print all dogs");
System.out.println("[2] Print all monkeys");
System.out.println("[3] Print all available animals");
System.out.println("[q] Exit");
userInput = scanner.nextLine();
while(!userInput.equals("q")) {
if(userInput.equals("1")) {
for(Dog dog : dogList) {
System.out.println("Name: " + dog.getName() + ", Training Status: " + dog.getTrainingStatus() +
", Acquisition Country: " + dog.getAcquisitionLocation() + ", Reserved Status: " + dog.getReserved());
}
return;
}
else if(userInput.equals("2")) {
for(Monkey monkey : monkeyList) {
System.out.println("Name: " + monkey.getName() + ", Training Status: " + monkey.getTrainingStatus() +
", Acuisition Country: " + monkey.getAcquisitionLocation() + ", Reserved Status: " + monkey.getReserved());
}
return;
}
else if(userInput.equals("3")) {
System.out.println("This option needs to be implemented.");
return;
}
else {
System.out.println("Please meake a valid selection.");
break;
}
}
}
}
【问题讨论】:
-
scanner.nextLine() 应该移到第一个 if 语句之前的 do-while 循环中。这是因为您希望始终在迭代发生之前获取用户输入
-
您在摄入方法中根本不需要遍历动物,只需读取动物并将其添加到列表中即可。
-
@tgdavies 是正确的。你为什么在intakeNewDog() 中使用for 循环?循环是完全没有必要的。这就是答案。完全删除循环。如果您想检查列表中是否存在狗的名字,请编写一个辅助方法 doesNameExist(String name) ,您可以在该方法中循环遍历列表。你不需要循环问题
-
这绝对解决了我的问题,非常感谢这些提示。我也将 printMenu() 方法移到了循环中,因为我意识到当循环结束时,intakeNewDog() 实际上并没有再次显示菜单。