1

我是初学者,几天来我一直在尝试调试我的代码,以找出为什么我在 isDuplicate 中的循环似乎无法正常工作。我已经在网站上搜索了一些想法,但似乎没有一个能解决我的问题。

目标是让用户输入一个项目,然后调用该方法来检查该项目是否已经在数组中。如果不是,则应添加。如果它是重复的,它应该给出一个错误消息。

我只能为此使用循环,因为目标是学习 java 中各种循环的实现。

当我运行程序时,它说第一个输入的项目是重复的。没有发现每个后续项目输入都是重复的。在我的 for 循环迭代或我忽略的 if 语句中似乎存在逻辑错误。

任何建议都非常感谢。我整个星期都试图在书本和网上寻找答案,但遇到了障碍。

/*
 Grocery List that accepts input, sorts by item name,
 checks for duplicates and outputs list to user.
 */
public class Main {

    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        int count = 0; // Number of items currently in the grocery list
        String[] groceryList = new String[6];

        /*
        invoke method to check for duplicate entries.
        use while loop for input from user
        */
        while (count < groceryList.length) {
            System.out.print("Enter a grocery item: ");
            String item = input.nextLine();
            isDuplicate(item, groceryList, count);
            groceryList[count] = item;
            count++;
        }

        // sort elements and use for-each loop to print list
        Arrays.sort(groceryList);
        System.out.println("Your Grocery List: ");
        for (String food : groceryList) {
            System.out.println(food);
        }
    }

    /*
       I know there is an issue with my loop but I could not figure it out.
       Says first element is duplicate but no others.
       Cannot use collections. can only use for loops for this method.
     */
    public static boolean isDuplicate(String item, String[] list, int listcnt) {
        for (listcnt = 0; listcnt < list.length; listcnt++) {
            for (int j = listcnt + 1; j < list.length; j++) {
                if (list[j] == list[listcnt])
                    System.out.println("Sorry, " + item + " is a duplicate.");
                return true;
            }
        }
        return false;
    }
}
4

3 回答 3

0

我不明白你的isDuplicate()方法。这是扫描数组中重复项的标准方法:

    public static boolean isDuplicate(String item, String[] list){
        //iterate the array
        for(String listItem : list){
            //check if "item" matches "listItem"
            if(item.equals(listItem)){
                return true;
            }
            //NOTE: if you want this to not be case sensetive,
            //e.g. if you want "somestring" to be equal to "SomeString", do
//            if(item.equalsIgnoreCase(listItem)){
//                return true;
//            }
        }
        //we only get this far in the method if the item is not in the list
        return false;
    }
于 2021-02-13T21:20:39.120 回答
0

暂时忘掉 Java 代码,从逻辑上思考它,然后在脑海中想出一个算法。

您有一个项目 - 尚未在列表中(这很重要)。你有一个项目列表。您想查看该项目是否已经在项目列表中。

对于纸上的清单,你会怎么做?

  • 扫描该项目的列表?听起来不错
  • 这是否需要循环遍历列表两次?没有

您也不会检查 isDuplicate 检查的结果并始终添加该项目

isDuplicate(item, groceryList, count);
groceryList[count] = item;

通常在 Java 中,我们不使用==比较对象:https ://www.geeksforgeeks.org/difference-equals-method-java/

if (list[j] == list[i])
于 2021-02-13T20:47:41.810 回答
0

您的主要问题是您不使用以下结果isDuplicate()

/*
 Grocery List that accepts input, sorts by item name,
 checks for duplicates and outputs list to user.
 */
public class Main {

    public static void main(String... args) {
        String[] groceryList = readGroceryList(6);
        // sort elements and use for-each loop to print list
        Arrays.sort(groceryList);
        print(groceryList);
    }

    private static String[] readGroceryList(int total) {
        Scanner scan = new Scanner(System.in);
        String[] groceryList = new String[total];
        int count = 0; // Number of items currently in the grocery list

        /*
        invoke method to check for duplicate entries.
        use while loop for input from user
        */
        while (count < groceryList.length) {
            System.out.print("Enter a grocery item: ");
            String item = scan.nextLine();

            if (isDuplicate(item, groceryList, count))
                System.err.println("Sorry, " + item + " is a duplicate.");
            else
                groceryList[count++] = item;
        }

        return groceryList;
    }

    private static void print(String[] groceryList) {
        System.out.println("Your Grocery List: ");

        for (String food : groceryList)
            System.out.println(food);
    }

    /*
       I know there is an issue with my loop but I could not figure it out.
       Says first element is duplicate but no others.
       Cannot use collections. can only use for loops for this method.
     */
    public static boolean isDuplicate(String item, String[] groceryList, int count) {
        for (int i = 0; i < count; i++)
            if (item.equals(groceryList[i]))
                return true;

        return false;
    }
}
于 2021-02-13T20:58:09.130 回答