【问题标题】:How do you repeat this method in array.length?你如何在array.length中重复这个方法?
【发布时间】:2021-05-26 05:29:41
【问题描述】:

在这个程序中,我希望用户输入 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] + " ");
    }
}

【问题讨论】:

  • 我看不出有任何理由期望所呈现的程序能够读取 any 输入。 searchProducts() 开头的代码读取一行,但该方法似乎从未被调用过,而且我看不到任何其他可以读取任何输入的代码..
  • 你的代码块不清楚,从哪里调用searhProduct?

标签: java arrays methods selection-sort


【解决方案1】:

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

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

(/*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;
}

【讨论】:

  • 对不起,我解决了这个问题,但仍然不允许我问 16 次。它只问我一次,然后当我按 Enter 键时,它会打印出 Unordered list 和 Ordered list 下的所有产品。
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2012-08-18
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多