gpt4 book ai didi

java - 如何过滤并显示列表?

转载 作者:行者123 更新时间:2023-12-01 13:46:47 25 4
gpt4 key购买 nike

我想按物种显示动物列表。我创建了一个方法 filterByS 来执行此操作,但它只是显示列表中的第一个宠物。

public abstract class Pet {
protected String id;
protected String name;
protected double weight;
protected Date date;
public Pet(){}
public Pet(String ma, String ten, double trongLuong, Date ngayNhap) {
this.id = ma;
this.name = ten;
this.weight = trongLuong;
this.date = ngayNhap;
}
public abstract int Filter(int tmp);

//

public class Lion extends Pet {
private double meatED;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Lion(){}
public Lion(String ma, String ten, double trongLuong, Date ngayNhap,
double khoiLuongThit) {
super(ma, ten, trongLuong, ngayNhap);
this.meatED = khoiLuongThit;
}
public int Filter(int tmp) {
// TODO Auto-generated method stub
return tmp=2;
}

//

public class Monkey extends Pet {
private String food;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Monkey(){}
public Monkey(String id, String name, double weight, Date date,
String loaiTAYT) {
super(id, name, weight, date);
this.food = loaiTAYT;
}
public int Filter(int tmp) {
return tmp=1;
}

//

public class Snake extends Pet{
private double length;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Snake(){}
public Snake(String ma, String ten, double trongLuong, Date ngayNhap,
double chieuDai) {
super(ma, ten, trongLuong, ngayNhap);
this.length = chieuDai;
}
public int Filter(int tmp) {
// TODO Auto-generated method stub
return tmp=3;
}

在 PetManament 类中,我调用 Filter 方法来过滤每个物种。

public class PetManament {
private List<Pet> listPet;

public PetManament() {
listPet = new ArrayList<Pet>();
}


public void filterByS(int k){
Iterator<Pet> iter=listPet.iterator();
while(iter.hasNext()){
Pet p=iter.next();
if(p.Filter(0)==k){
System.out.println(p.toString());
}
else if(p.Filter(0)==k){
return;
}
else if(p.Filter(0)==k){
return;
}
else return;
}
}

在类程序中我称之为filterByS。

public class Program {
static PetManament list=new PetManament();
public static void main(String[] args) {
int stepm=1;
do{
System.out.println("");
System.out.println("(1): Add new");
System.out.println("(2): Remove");
System.out.println("(3): edit pet information ");
System.out.println("(4): Search by id or name ");
System.out.println("(5): list");
System.out.println("(6): show species list");
System.out.println("(7): ");
System.out.println("(8): ");
Scanner s = new Scanner(System.in);
int step=s.nextInt();
switch(step){
case 1:
AddNew();
break;
case 2:
Remove();
break;
case 3:
break;
case 4:
TimKiem();
break;
case 5:
PrintL();
break;
case 6:
filterByS();
break;
case 7:
break;
case 8:
stepm=0;

}
}while(stepm==1);
}

private static void filterByS() {
Scanner s = new Scanner(System.in);
int k=0;
System.out.println("(1): show list monkey"+"\n(2) show list lion"+"\n(3)show list lion"+"\nback to menu");
k=s.nextInt();
switch(k){
case 1:
list.filterByS(k);
break;
case 2:
list.filterByS(k);
break;
case 3:
list.filterByS(k);
break;
default:
break;
}

}

最佳答案

那是因为在 PetManament 类的 filterByS 中,如果您打印 p.Filter(0)==k ,则无论出于何种原因后面有一些相同的条件,除了返回它们(但它们永远不会到达),但如果不是这种情况,您返回

关于java - 如何过滤并显示列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20310144/

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