0

所以我做了这个扫描仪,我希望它可以接收 1 到 3 个字符串。1 是它可以使用的最小字符串数,3 是最大字符串数。到目前为止,我的输出是:

You can search 1 - 3 words only : 3
Type a name: word1 
Type a name: word2
Type a name: word3

相反,有没有办法可以将这些单词放在多个数组中,因为我的程序将执行多个字符串搜索。

public static void main(String[]args) throws IOException{

    int nnames;
    String[] stringSearch;

    System.out.print("You can search 1 - 3 words only : ");
    Scanner in = new Scanner(System.in);
    nnames = Integer.parseInt(in.nextLine().trim());

    stringSearch = new String[nnames];
    for (int i = 0; i < stringSearch.length; i++){
            System.out.print("Type a name: ");
            stringSearch[i] = in.nextLine();

    }
4

1 回答 1

0

像这样的东西?

    public static void main(String[]args) throws IOException{
        int nnames;
        int ind = 0;
        int maxSearch = 10000;//this is max times we can do searching
        String[][] stringSearch = new String[maxSearch][];

        System.out.print("You can search 1 - 3 words only(else means stop search) : ");
        Scanner in = new Scanner(System.in);
        nnames = Integer.parseInt(in.nextLine().trim());

        while(nnames>=1 && nnames<=3){
            stringSearch[ind] = new String[nnames];
            for (int i = 0; i < stringSearch.length; i++){
                System.out.print("Type a name: ");
                stringSearch[ind][i] = in.nextLine();
            }
            ind++;
            nnames = Integer.parseInt(in.nextLine().trim());
        }
    }
于 2013-01-10T14:09:38.830 回答