If statements 运行 simultaneously, if ArrayList 中的两个对象

If statements running simultaneously, if two objects in ArrayList

下面是代码。我无法弄清楚为什么在 class 数据库中我的 else if 状态与我的 if 语句同时运行 IF 我已经将第二只鸟输入到 ArrayList 中。请提供任何帮助,我们将不胜感激!

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Database d1 = new Database();

        while (true) {
            System.out.println("What do you want to do?");
            String answer = input.nextLine();

            if(answer.equalsIgnoreCase("Add")){
                System.out.println("Name: ");
                String name = input.nextLine();
                System.out.println("Latin name: ");
                String lName = input.nextLine();

                d1.addBird(name, lName);
            } else if (answer.equalsIgnoreCase("O")) {
                System.out.println("What was observed?");
                String observed = input.nextLine();
                d1.Observation(observed);
            } else if (answer.equalsIgnoreCase("stats")) {
                d1.showBirds(); //Displays all with observations.
            } else if (answer.equalsIgnoreCase("show")) {
                System.out.println("What?");
                String search = input.nextLine();
                d1.searchBird(search);
            } else if (answer.equalsIgnoreCase("quit")){
                break;
            }
        }
    }
}

public class Bird {

    private final String name;
    private final String latinName;
    private int count;

    public Bird (String name, String latinName) {
        this.name = name;
        this.latinName = latinName;
        this.count = count;
    }

    public String getName () {
        return this.name;
    }

    public String getLatin() {
        return this.latinName;
    }

    public String add () {
        return this.name + " " + "(" +this.latinName + ")"+ " " + this.count + " observation(s)";
    }

    public void increaseCount () {
        this.count++;
    }
}

import java.util.ArrayList;

public class Database {

    private final ArrayList<Bird> birdList;

    public Database() {
        this.birdList = new ArrayList<Bird>();
    }

    public void addBird (String name, String lname) {
        this.birdList.add(new Bird(name, lname));
    }

    public void Observation (String observed) {
        for (Bird x : getBirds()) { // this has to be a method
            if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
                System.out.println("Done");
                System.out.println("");
                x.increaseCount();      
            } else if (x.getName() != observed || x.getLatin() != observed) {
                System.out.println("Not a bird");
            }
        }
    }

    public void showBirds () {
        for (Bird x : this.birdList) {
            System.out.println(x.add());
        }
    }

    public ArrayList<Bird> getBirds() {
        return this.birdList;
    }

    public void searchBird(String search) {
        for (Bird x : getBirds()) {
            if (x.getName().contains(search)) {
                System.out.println(x.add());
            }
        }
    }
}

我觉得问题出在这个方法上:

public void Observation (String observed) {

    for (Bird x : getBirds()) { // this has to be a method
        if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
            System.out.println("Done");
            System.out.println("");
            x.increaseCount();      
        } 
         /* No need to print "Not a bird" for every mismatch. Use a flag instead */
        else if (x.getName() != observed || x.getLatin() != observed) {
            System.out.println("Not a bird");
        }
    }
}

做这样的事情:

public void Observation (String observed) {
    boolean found = false;
    for (Bird x : getBirds()) { // this has to be a method
        if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
            System.out.println("Done");
            System.out.println("");
            x.increaseCount();
            found = true;      
        } 
    }
    if (!found) {
            System.out.println("Not a bird");
    }
}