我应该创建一个类似于此的输出:
我已将数据组织成一个数组。简单地说,我把它组织起来了 8个不同的POIBook及其个人数据。
POIBook[i] = new POI(Type, Place, Rating)
根据用户输入JOptionpane
显示,调用正确的数组没有问题。
但是,我在尝试确定用户当前所在的搜索总数和搜索结果时遇到问题。例如。搜索1 of 3
如何获取搜索结果的总数和用户当前所在的数字?
编辑:添加问题的上下文。
用户将在JOptionpane
中输入搜索查询,它将搜索并返回包含搜索结果的数组。例如,搜索“Excitement”将返回POIBook [4],[5]和[6]作为JOptionpane
显示,并按照上图中预期的输出。
需要以下方面的帮助:
当POIBook [4]返回时,如何显示'搜索结果 - 1/3'?当POIBook [5]被退回等时,“搜索结果 - 2/3”?
在显示时,我们需要知道有3种“兴奋”类型的结果,它目前显示3种中的第1项结果。
public static void dataPOI() {
POIBook[0] = new POI("Sightseeing", "River Safari","Wildlife, Panda Bear, Cruise",4.0);
POIBook[1] = new POI("Sightseeing", "Singapore Flyer","Fly!",3.5);
POIBook[2] = new POI("Sightseeing", "Gardens by The Bay","Flower Dome, Cloud Forest",3.5);
POIBook[3] = new POI("Shopping", "Orchard Road","Ion, Mandarin Gallery",4.0);
POIBook[4] = new POI("Excitement", "Universal Studios","Battlestar Galactica, Puss in Boots",4.5);
POIBook[5] = new POI("Excitement", "Marina Bay Sands","Casino, Sky Park Observation Deck",4.0);
POIBook[6] = new POI("Excitement", "Resorts World Sentosa","Trick Eye Museum, Adventure Cove",4.5);
POIBook[7] = new POI("Night Life", "Night Safari","Wildlife, Tram Ride",4.5);
}
我的代码如下。
public static void search() {
String search = JOptionPane.showInputDialog(null, "Please enter your type of attraction", "Place of Interest",
JOptionPane.QUESTION_MESSAGE);
for (int i = 0; i<POIBook.length; i++) {
if(search.equalsIgnoreCase(POIBook[i].type)) {
Object[] options = { "Next >", "Return to Menu" };
int select = JOptionPane.showOptionDialog(null, "Place of interest " + (i+1) + " of 8\n" + POIBook[i].display() +
"\nPress Next for next Place of Interest \n" + "To return, press Return to Menu \n", "Display Places of Interest in Sequence",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE, POIBook[i].icon, options, options[1]);
if (select == 1) {
return;
}
}
}
}