这是我现在的代码。我真的一直在仔细研究整个事情,但为了生活的缘故,我无法找到让代码无法工作的问题在于:
import java.util.ArrayList;
import java.util.Scanner;
public class DogGompundTest {
public static void main(String[] args) {
boolean dogFound = false;
boolean toEnd = false;
Scanner keyboard = new Scanner(System.in);
ArrayList<Dog> dogRegister = new ArrayList<Dog>();
//Other dogs in program
Dog d2 = new Dog("Mira", "Miniature Schnauzer", 1, 8.0, 0.8);
dogRegister.add(d2);
Dog d3 = new Dog("Jack", "Jack Russell", 3, 6.0, 1.8);
dogRegister.add(d3);
Dog d4 = new Dog("Charlie", "Pug", 5, 5.0, 2.5);
dogRegister.add(d4);
Dog d5 = new Dog("Max", "Dachshund", 9, 5.0, 3.7);
dogRegister.add(d5);
Dog d6 = new Dog("Bingo", "Golden Retriever", 5, 12.0, 6.0);
dogRegister.add(d6);
System.out.println("Name - Race - Age - Weight - Taillength: " + dogRegister);
while (toEnd == false) {
System.out.println(
"\nWhat would you like to do? \n Press 1 to register a dog. \n Press 2 to get a " +
"look at the taillengths of the dogs. \n Press 3 to delete a dog from " +
"the register.\n Press 4 to quit.");
int command = keyboard.nextInt(); //Alternatives stored in "command"
keyboard.nextLine();
switch (command) { //Execute chosen command in switch-statement
case 1: //User registers a dog
Dog d1 = new Dog("", "", 0, 0.0, 0.0);
System.out.println("\nPlease enter the dogs name:");
String Name = keyboard.next();
d1.setName(Name);
System.out.println("\nPlease enter the dogs race (in English):");
String Race = keyboard.next();
d1.setRace(Race);
System.out.println("\nPlease enter the dogs age (years):");
int age = keyboard.nextInt();
d1.setage(age);
System.out.println("\nPlease enter the dogs weight in kg:");
double weight = keyboard.nextDouble();
d1.setweight(weight);
dogRegister.add(d1);
System.out.println("\n");
System.out.println(
"\nComplete dog information: " + "\nName: " + Name + "\nRace: " + Race
+ "\nAge: " + age + "\nWeight: " + weight + " kg");
System.out.println(dogRegister);
break;
case 2: //User gets to see the different taillengths of the dogs in the register
System.out.println(
"\nEnter taillength and all dogs with a greater taillength will be displayed: ");
double taillength = keyboard.nextDouble();
for (int index = 0; index < dogRegister.size(); index++) {
if (taillength <= (dogRegister.get(index).gettaillength())) {
System.out.println(dogRegister.get(index));
}
}
break;
case 3: //User deletes a dog from the register
System.out.println("State the name of the dog you wish to delete from the register: ");
String delete = keyboard.nextLine();
for (int del = 0; del < dogRegister.size(); del++) {
if (delete.equalsIgnoreCase(dogRegister.get(del).getName())) {
System.out.println("\nThe dog with the given name has been deleted from the register.");
dogFound = true;
dogRegister.remove(del);
System.out.print(dogRegister + "\n");
}
}
{
if (!dogFound)
;
System.out.println("A dog by that name is not registered in our system.");
}
break;
case 4:
System.out.println("\nThe program has now ended.");
toEnd = true;
}
}
}
}
答案 0 :(得分:1)
您需要在循环外移动else { }
块并使用第二个变量来确定是否找到了现有的狗,然后将其设置为true。然后,您的else { }
数据块会显示为if(!dogFound) { }
否则你的代码会在遇到不是被寻找者的每只狗时打印“未找到”行,因为我确信你看到了。 e.g。
dogs = ["Jack","Pooch","Fido","Fluffy"]
如果您尝试删除“Fluffy”,执行将如下所示:
"Jack" isn't "Fluffy", print "not found."
"Pooch" isn't "Fluffy", print "not found."
"Fido" isn't "Fluffy", print "not found."
"Fluffy" is "Fluffy", remove and print "removed."