在 arrayList 中打印与布尔响应相反的内容

Print the opposite of a boolean response in an arrayList

我正在从事一个保护动物的项目。预订动物时,​​我想打印出可用动物的名称列表。 ArrayList 有几个元素,包括名称和是否保留。我想我需要的是打印可用的小动物。我对使用 Java 很陌生,这个网站对我帮助很大,但我似乎无法获得任何我发现的可以按我需要的方式工作的东西。

initializeDogList();
initializeMonkeyList();
// 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", "in service", 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("Clyde", "Guenon", "5.1", "8.4", "19.6", "male", "2", "15.3", "08-21-2019", "Australia", "Phase III", true, "Canada");
    Monkey monkey2 = new Monkey("Chia", "Macaque", "4.8", "10.2", "19.3", "female", "1", "13.4", "11-15-2018", "United Kingdom", "in service", false, "United Kingdom");
    Monkey monkey3 = new Monkey("Pat", "Tamarin", "5.5", "10.6", "13.7", "female", "3", "18.2", "01-04-2021", "United States", "intake", false, "United States");

    monkeyList.add(monkey1);
    monkeyList.add(monkey2);
    monkeyList.add(monkey3);
}


public static void reserveAnimal(Scanner scanner) {
        System.out.println("Enter the animal type");
        String type = scanner.nextLine();

        if (type.equals("Monkey") || type.equals("monkey")) {
            for (int i = 0; i < monkeyList.size(); i++) {
                if (monkeyList.get(i).getReserved());
                System.out.println(monkeyList.get(i));
            }
            System.out.println("Enter name: ");
            String name = scanner.nextLine();
            for (Monkey obj: monkeyList) {

                if (obj.name.equals(name)) {
                    obj.reserved = true;
                    return;
                }
            }
            System.out.println("Sorry that Monkey is not in our list");
        }
        if (type.equals("Dog") || type.equals("dog")) {
            for (int i = 0; i < dogList.size(); i++) {
                if (dogList.get(i).getReserved())
                    System.out.println(dogList.get(i));
            }
            System.out.println("Enter name: ");
            String name = scanner.nextLine();
            for (Dog obj: dogList) {
                if (obj.name.equals(name)) {
                    obj.reserved = true;
                    return;
                }
            }
            System.out.println("Sorry that Dog is not in our list");
        }
}

我不确定我是否理解你的问题,但如果你想反转布尔变量,只需使用 !variable

如果您想恢复布尔值,只需在它前面使用 !(也称为 NOT 条件)。

例如

!monkeyList.get(i).getReserved()