你如何在array.length 中重复这个方法?

问题描述

在这个程序中,我希望用户输入 20 个产品名称。从主要方法。这将传递给名为 searchProducts 的方法。但由于某种原因,它不起作用。它只让我输入一次,然后打印出所有 16 个产品。

public static void main(String[] args) {
    String[] products= {"Pencil pouch","pen","Pencil sharpener","High lighters","Markers","Erasers","Binder","Notebooks","Index cards","Folders","glue","Ruler","Scissors","Calculator","Calendar","Backpack"};

    System.out.println("Unordered list");
    displayProducts(products);
    sortProducts(products,16);
    System.out.println("");
    System.out.println("Ordered list");
    displayProducts(products);
}

 private static int searchProducts(String[] products) {

    Scanner sc = new Scanner(system.in);
    String x = sc.nextLine();

    System.out.println("Enter name of product: ");
    for (int i = 0; i < products.length; i++) {
        if (products[i].equals(x))
            return i;
    }
    return -1;
}


private static void sortProducts(String products[],int n) {
    for(int i = 0; i < n - 1; i++) {
        int minindex = i;
        String minStr = products[i];

        for(int j = i + 1; j < n; j++) {
            if(products[j].compareto(minStr) < 0)
            {
                minStr = products[j];
                minindex = j;
            }
        }

        if(minindex != i)
        {
            String temp = products[minindex];
            products[minindex] = products[i];
            products[i] = temp;
        }
    }
}

private static void displayProducts(String[] products) {

    for(int i = 0; i < products.length; i++){

        System.out.println(products[i] + " ");
    }
}

解决方法

传递数组参数的方式很好。

有几种方法可以将数组作为参数传递:

(/*Other params,*/ String[] param) // this way
(String[] param /*,Other params*/) // or

(/*Other params,*/ String param[]) // this way
(String param[] /*,Other params*/) // or

// special case
// only as unique or last param of the params 
// because with it you can enter several String params as individuals 
(/*Other params,*/ String... param)


// arrays of arrays
(String[] param[])
(String[][] param)
(String param[][] ) 

这是你的问题:

sortProducts(products,20);
...
private static void sortProducts(String products[],int n) {

尽管您的数组大小为 16,但您通过了 20。所以错误。

改变这种方式不依赖于大小。

     sortProducts(products);
     ....
     private static void sortProducts(String products[]) { // no size passed
        int n = products.length; // read the size from the array
    

编辑 1 -------------

在下面的代码中,用户输入N个产品。然后打印、排序和打印数组。 (未测试)

public static void main(String[] args) {
    int N = 20;
    String[] products = new String[N];

    Scanner sc = new Scanner(System.in);
    for(int i=0; i < N; i++) {
       System.out.println("Enter name of product "+ (i+1) +" : ");
       String x = sc.nextLine();
       products[i] = x;
    }

    System.out.println("Unordered list");
    displayProducts(products);
    sortProducts(products);
    
    System.out.println("");
    System.out.println("Ordered list");
    displayProducts(products);

    // search block
}

要在数组中搜索,您可以执行类似操作(未测试):

public static void main(String[] args) {

    // ... previous code



    // search block in main method
    System.out.println("Enter name of product to search,or \"stop\" to stop : ");
    String y = sc.nextLine();
    while(y != "stop") {
       int index = searchProducts(products,y);

       if( index == -1 )
           System.out.println("Product "+ y +" is not in array");
       else
           System.out.println("Product "+ y +" is at position "+ index);


       System.out.println("Enter name of product to search,or \"stop\" to stop : ");
       y = sc.nextLine();
    }
}

private static int searchProducts(String[] products,String p) {

    for (int i = 0; i < products.length; i++) {
        if (products[i].equals(p))
            return i;
    }
    return -1;
}