抱歉,我是Java的新手,可能有一个非常简单的答案。
目前,我将所有结果打印在个人JOptionPane
中
我想设置一个搜索功能,以便根据给定的标准显示某些结果。
到目前为止,这是我的代码:
public class Main {
public static void main(String[] args) {
//Create new Person objects
Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Details c1 = new Details();
Details c2 = new Details();
Details c3 = new Details();
//Send some messages to the objects
c1.setBeds("3 ");
c2.setBeds("6");
c3.setBeds("4");
c1.setPrice("175,000");
c2.setPrice("300,00");
c3.setPrice("250,000");
c1.setType("Terraced");
c2.setType("Bungalow");
c3.setType("Detached");
//Set up the association
p1.ownsDetails(c1);
p2.ownsDetails(c2);
p3.ownsDetails(c3);
//Print result
p1.printDetails();
p2.printDetails();
p3.printDetails();
//Finally quit
System.exit(0);
}
}
任何帮助都将不胜感激,谢谢。
答案 0 :(得分:1)
您可能会发现拥有一个数组非常有用,而不是拥有三个独立的变量:
Address p[] = new Address[3];
p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
现在可以编写一个循环来检查每一个:
for(int i = 0; i < p.length; i++) {
if (p[i].getSomething().equals(thingToSearch)) {
// ... it's a match!
}
}
答案 1 :(得分:1)
是的,您可以创建一个自定义JDialog
或JFrame
,其中包含多个输入字段和多个搜索结果。
理想情况下,您的对象需要在列表或数组中 才能进行搜索,
这样你就可以遍历所有这些并找到匹配。