gpt4 book ai didi

java - 通过搜索类变量来删除 ArrayList 中的元素

转载 作者:行者123 更新时间:2023-12-01 19:43:58 26 4
gpt4 key购买 nike

我正在尝试在 ArrayList 中搜索一只狗,然后根据调用的方法删除它或增加它的年龄。

现在我只能删除/增加 ArrayList 上第一只狗的年龄。当我搜索 Turbo 或 Kasper 时,它们不会出现。我认为由于某种原因他们没有被循环击中。

主类:

public class main {
public static void main(String[] args) {
DogShell dogShell = new DogShell();
Dog fidoDog = new Dog("Fido", "schäfer", 7, 27);
Dog turboDog = new Dog("Turbo", "bulldog", 12, 33);
Dog kasperDog = new Dog("Kasper", "sheepdog", 5, 4);
dogShell.dogArrayList.add(fidoDog);
dogShell.dogArrayList.add(turboDog);
dogShell.dogArrayList.add(kasperDog);


dogShell.initialize();
dogShell.runCommandLoop();
dogShell.shutDown();
}
}

狗类:

public class Dog {
// Fields
private String name, breed;
private int age;
private int weight;

// Constructor
public Dog(String name, String breed, int age, int weight) {
this.name = name;
this.age = age;
this.breed = breed;
this.weight = weight;
}

// Methods
public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getBreed() {
return breed;
}

public int getWeight() {
return weight;
}

public double getTailLength() {
double taxTailLength = 3.7;
double tailLength = ((double) age * (double) weight) / 10;
if (breed.equalsIgnoreCase("tax") || breed.equalsIgnoreCase("dachshund")) {
return taxTailLength;
} else {
return tailLength;
}
}

public String toString() {
String result = "";
result ="\nName: " + name + "\nBreed: " + breed + "\nAge: " + age + " years" + "\nWeight: " + weight + " kg" + "\nTail length: " + getTailLength() + "\n";
return result;
}
// Increases age by one year.
public int increaseAge() {
return this.age++;
}
}

DogShell 类:

import java.util.*;

public class DogShell {

ArrayList<Dog> dogArrayList = new ArrayList<Dog>();
Iterator<Dog> iterator = dogArrayList.iterator();
Scanner scanner = new Scanner(System.in);

public DogShell() {
}

public void initialize() {
System.out.println("Welcome to the dog register!");

}

public void runCommandLoop() {
Scanner scanner = new Scanner(System.in);
String input;
do {
System.out.print("Command> ");
input = scanner.nextLine();
switch (input) {
case "exit":
break;
case "remove dog":
removeDogs();
break;
case "list dogs":
listDogs();
break;
case "increase age":
increaseAge();
break;
case "register new dog":
registerDog();
break;
default:
System.out.println("Error: unknown command " + "\"" + input + "\"");
break;
}
} while (!input.equalsIgnoreCase("exit"));

}

public void shutDown() {
System.out.println("Goodbye!");
}

public void registerDog() {
Scanner scanner = new Scanner(System.in);
System.out.println("You gave the command \"register new dog\"");
System.out.println("Name: ");
String dName = scanner.nextLine();
System.out.println("Breed: ");
String dBreed = scanner.nextLine();
System.out.println("Age: ");
int dAge = scanner.nextInt();
System.out.println("Weight: ");
int dWeight = scanner.nextInt();

dogArrayList.add(new Dog(dName, dBreed, dAge, dWeight));
}

public void listDogs() {
System.out.println("You gave the command \"list dogs\"");
System.out.println(Arrays.toString(dogArrayList.toArray()));
}

public void increaseAge() {
System.out.println("Enter the name of the dog you want to age: ");
String name = scanner.nextLine();
for (int i = 0; i < dogArrayList.size(); i++) {
String dogName = dogArrayList.get(i).getName();
if (name.equalsIgnoreCase(dogArrayList.get(i).getName())) {
dogArrayList.get(i).increaseAge();
System.out.println(dogArrayList.get(i).getName() + " is now " + dogArrayList.get(i).getAge() + " years old");
break;
} else {
System.out.println("Error: no dog called " + name);
break;
}
}
}

public void removeDogs() {
String name;
System.out.println("What is the name of the dog you want to delete");
name = scanner.nextLine();
for (int i = 0; i < dogArrayList.size(); i++) {
String dogName = dogArrayList.get(i).getName();
if (name.equalsIgnoreCase(dogArrayList.get(i).getName())) {
dogArrayList.remove(dogArrayList.get(i));
System.out.println(name + " has been deleted.");
break;
} else {
System.out.println("Error: no dog with " + name + " name.");
break;
}
}
}


}

执行和测试时的输出

Welcome to the dog register!

Command> list dogs

You gave the command "list dogs"

[

Name: Fido

Breed: schäfer

Age: 7 years

Weight: 27 kg

Tail length: 18.9

,

Name: Turbo

Breed: bulldog

Age: 12 years

Weight: 33 kg

Tail length: 39.6

,

Name: Kasper

Breed: sheepdog

Age: 5 years

Weight: 4 kg

Tail length: 2.0

]

Command> register new dog

You gave the command "register new dog"

Name:

stack

Breed:

overflow

Age:

13

Weight:

37

Command> list dogs

You gave the command "list dogs"

[

Name: Fido

Breed: schäfer

Age: 7 years

Weight: 27 kg

Tail length: 18.9

,

Name: Turbo

Breed: bulldog

Age: 12 years

Weight: 33 kg

Tail length: 39.6

,

Name: Kasper

Breed: sheepdog

Age: 5 years

Weight: 4 kg

Tail length: 2.0

,

Name: stack

Breed: overflow

Age: 13 years

Weight: 37 kg

Tail length: 48.1

]

Command> increase age

Enter the name of the dog you want to age:

stack

Error: no dog called stack

Command> increase age

Enter the name of the dog you want to age:

fido

Fido is now 8 years old

Command> remove dog

What is the name of the dog you want to delete

stack

Error: no dog with stack name.

Command> remove dog

What is the name of the dog you want to delete

fido

fido has been deleted.

Command> list dogs

You gave the command "list dogs"

[

Name: Turbo

Breed: bulldog

Age: 12 years

Weight: 33 kg

Tail length: 39.6

,

Name: Kasper

Breed: sheepdog

Age: 5 years

Weight: 4 kg

Tail length: 2.0

,

Name: stack

Breed: overflow

Age: 13 years

Weight: 37 kg

Tail length: 48.1

]

Command> exit

Goodbye!

最佳答案

这是因为你太不耐烦了。 :-)

for (int i = 0; i < dogArrayList.size(); i++) {
String dogName = dogArrayList.get(i).getName();
if (name.equalsIgnoreCase(dogArrayList.get(i).getName())) {
dogArrayList.get(i).increaseAge();
System.out.println(dogArrayList.get(i).getName() + " is now " + dogArrayList.get(i).getAge() + " years old");
break;
} else {
System.out.println("Error: no dog called " + name);
break;
}
}

如果比较失败,则不会给循环机会前进到下一个项目,但您已经输出没有找到狗。

相反,你应该这样做

for (int i = 0; i < dogArrayList.size(); i++) {
String dogName = dogArrayList.get(i).getName();
if (name.equalsIgnoreCase(dogArrayList.get(i).getName())) {
dogArrayList.get(i).increaseAge();
System.out.println(dogArrayList.get(i).getName() + " is now " + dogArrayList.get(i).getAge() + " years old");
break;
}
}
// Only now, after the loop, we are sure that there is no dog with the given name.
System.out.println("Error: no dog called " + name);

关于java - 通过搜索类变量来删除 ArrayList 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54242666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com