简体   繁体   English

Java - 使用 ArrayList/HashSet 查询的搜索结果进行菜单选择

[英]Java - use search results from ArrayList/HashSet query for menu selection

I'm wondering if anyone can give me some pointers on what the next steps in a basic program I have.我想知道是否有人可以给我一些关于我拥有的基本程序的下一步的指示。

I have the following example java code:我有以下示例 java 代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    ArrayList<Transport> transportList = new ArrayList<>();
    Scanner input = new Scanner(System.in);

    public void addToList() {
        transportList.add(new Transport("Blue", "Large", "Plane"));
        transportList.add(new Transport("Red", "Small", "Car"));
        transportList.add(new Transport("Brown", "Large", "Train"));
        transportList.add(new Transport("Yellow", "Small", "Boat"));
        transportList.add(new Transport("Yellow", "Small", "Plane"));
        transportList.add(new Transport("Brown", "Large", "Car"));
        transportList.add(new Transport("Red", "Large", "Train"));
        transportList.add(new Transport("Blue", "Small", "Boat"));

        searchColour();
    }

    public void searchColour() {
        System.out.println("Enter a colour to search:\n");
        String colourSearch = input.next();
        for (Transport transport : transportList) {
            if (transport.getColour().toLowerCase().equals(colourSearch.toLowerCase())) {
                System.out.printf(
                    "There is a %s %s that is %s \n",
                    transport.getSize(),
                    transport.getType(),
                    transport.getColour()
                );
            }
        }
        searchColour();
    }

    public static void main(String[] args) {
        new Main().addToList();
    }
}
public class Transport {

    private String colour;
    private String size;
    private String type;

    // All-args constructor, getters and setters omitted for brevity
}

This runs and prompts the user to enter a colour.这将运行并提示用户输入颜色。 It then searches the ArrayList and outputs the matches.然后它搜索 ArrayList 并输出匹配项。 What I now need to do is to take the results I get from a colour search and reuse them in a menu for selection.我现在需要做的是获取从颜色搜索中获得的结果,并在菜单中重新使用它们以供选择。

For example if I search for "red", I get:例如,如果我搜索“红色”,我会得到:

There is a Small Car that is Red有一辆红色的小车

There is a Large Train that is Red有一辆红色的大火车

What I want is to have a number placed in front of them and another prompt to choose which one the person whats.我想要的是在他们面前放一个数字,并提示选择那个人是什么。 So it should say something like:所以它应该这样说:

  1. There is a Small Car that is Red有一辆红色的小车
  2. There is a Large Train that is Red有一辆红色的大火车

"Choose which transport you want" “选择你想要的交通工具”

Outputting the next question and prompting for an input with Scanner I can do, I'm not sure how I can add numbers to the start of the results, knowing that as my ArrayList grows the number of items will, and how I do a selection choice from it so a user can press 1 or 2.输出下一个问题并提示输入我可以使用的扫描仪,我不知道如何在结果的开头添加数字,因为我知道随着我的 ArrayList 增加项目数量,以及我如何进行选择从中选择,以便用户可以按 1 或 2。

Can anyone offer a suggestion?任何人都可以提供建议吗? I'm thinking I need to use another ArrayList but I'm not sure how this should be structured.我想我需要使用另一个 ArrayList 但我不确定这应该如何构建。

I think you should make use of the fact, that an ArrayList keeps the elements in insertion order.我认为您应该利用一个事实,即 ArrayList 将元素保持在插入顺序中。 You could eg use the "indexOf(Object o)" method to get the number you want to be printed.例如,您可以使用“indexOf(Object o)”方法来获取要打印的数字。 (You have to add 1 to get your example, because it starts with 0.) (您必须添加 1 才能获得示例,因为它以 0 开头。)

And to select the item you can use "get(int index)" and let the user type the index.对于 select 项目,您可以使用“get(int index)”并让用户键入索引。 (here you have to substract 1, like above.) (这里你必须减去 1,就像上面一样。)

What you want is a counter.你想要的是一个计数器。 Create an int variable to keep track of the question number.创建一个int变量来跟踪问题编号。

When the user inputs a number, use the List's get method to retrieve the choice:当用户输入一个数字时,使用 List 的get方法来检索选择:

public void searchColour() {
    System.out.println("Enter a colour to search:\n");
    String colourSearch = input.next();

    int questionNumber = 1;
    for (Transport transport : transportList) {
        if (transport.getColour().toLowerCase().equals(colourSearch.toLowerCase())) {
            System.out.printf(
                "%d. There is a %s %s that is %s \n",
                questionNumber++,
                transport.getSize(),
                transport.getType(),
                transport.getColour()
            );
        }
    }

    int choice;
    do {
        System.out.println();
        System.out.println("Choose which transport you want:");
        choice = input.nextInt();
    } while (choice < 1 || choice > transportList.size());

    // List indices start at zero, not one.
    Transport transport = transportList.get(choice - 1);

    System.out.printf("You chose a %s %s that is %s%n",
                transport.getSize(),
                transport.getType(),
                transport.getColour()
}

The println method prints a newline after printing the argument you pass it. println方法在打印您传递的参数后打印一个换行符。 Therefore, you don't need to pass a String ending with \n , unless you really want to print a blank line after the text (and that may not work in Windows, where a newline sequence is actually the two characters \r\n ).因此,您不需要传递以\n结尾的字符串,除非您真的想在文本之后打印一个空行(这在 Windows 中可能不起作用,其中换行符序列实际上是两个字符\r\n )。

In the printf method, you can use the special Formatter sequence %n instead of a literal \n to make sure the system's newline sequence is always used.printf方法中,您可以使用特殊的格式化程序序列%n而不是文字\n以确保始终使用系统的换行符序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM