0

我正在尝试编写一种删除 CD 的方法(CD 包括艺术家姓名、专辑标题和曲目标题).. 有 5 张 CD,我想删除其中... 这就是该方法应该做的: void delete()将 1) 向用户询问艺术家和标题,然后尝试查找与艺术家和标题匹配的 CD,2) 如果找到则显示 CD,或者告诉用户它没有找到,以及 3) 如果找到,要求用户确认删除(这需要键盘输入),如果用户确认删除CD条目。

这是我的代码:

public void delete() {
    Scanner deleteInput = new Scanner(System.in);
    System.out.println("Which artist you would like to delete? ");
    System.out.println("Enter artist name and title to be deleted:");
    String artist = deleteInput.nextLine();
    String title = deleteInput.nextLine();

    for (int i = 0; i <= CDlist.length - 1; i++) {

        if ((CDlist[i].getArtist().equals(artist))
                && (CDlist[i].getTitle().equals(title))) {
            System.out.println("Found: " + CDlist[i].getArtist() + " "
                    + CDlist[i].getTitle());
            if (CDlist[i] == null) {
                continue;
            }

            System.out.println("Would you like to delete it? Yes 0 No 1");                
            if (deleteInput.nextInt() == 1) {
                CDlist[i] = null;
                cdnum--;
            }
        } else {
            System.out.println("CD not found in the list.");
        }
    }

我的问题是,当我键入要删除的正确艺术家和标题时,我得到的输出为 CD 未找到(但它应该说找到然后删除它)请问我该如何解决这个问题?

4

1 回答 1

0

这就是我的意思。此外,在 for 循环开始后{,您的第一个文件中还有一个额外的内容。if我希望这不会造成问题。

我根据一些评论添加了忽略案例部分。

public void delete() {
    Scanner deleteInput = new Scanner(System.in);
    System.out.println("Which artist you would like to delete? ");
    System.out.println("Enter artist name and title to be deleted:");
    String artist = deleteInput.nextLine();
    String title = deleteInput.nextLine();

    boolean found = false;
    int idx = -1;

    System.err.println("DEBUG: Input Data");
    System.err.println("Artist Name: "+artist+" Length of String: "+artist.length());
    System.err.println("Title: "+artist+" Length of String: "+title.length());
    System.err.println();

    for (int i = 0; i <= CDlist.length - 1; i++) {

        if (CDlist[i]!=null) {
            System.err.println("DEBUG: Checking Index "+i);
            System.err.println("Artist Name: "+CDlist[i].getArtist()+" Length of String: "+CDlist[i].getArtist().length() + " Matches: "+CDlist[i].getArtist().equalsIgnoreCase(artist));
            System.err.println("Title: "+CDlist[i].getTitle()+" Length of String: "+CDlist[i].getTitle().length() + " Matches: "+CDlist[i].getTitle().equalsIgnoreCase(title));
            System.err.println();
        }

        if (CDlist[i]!=null && CDlist[i].getArtist().equalsIgnoreCase(artist) && CDlist[i].getTitle().equalsIgnoreCase(title)) {
            System.out.println("Found: " + CDlist[i].getArtist() + " " + CDlist[i].getTitle());
            found = true;
            idx = i;
                    break;
        }
    }

    if (found) {
        System.out.println("Would you like to delete it? Yes 0 No 1");                
        if (Integer.parseInt(deleteInput.nextLine()) == 1) {
            CDlist[idx] = null;
            //I am assuming cdnum is a variable of the class that can be accessed.
            cdnum--;
        }
    } else {
        System.out.println("CD not found in the list.");
    }
}

注意:您不需要将 CDlist[i] 单独放在 if 语句中,您可以合并到另一个语句中。之所以可以这样做,是因为 java 会查看 left-> right 的内容,因此它会在尝试执行任何操作之前检查 null。您可以单独使用它,但您需要将它放在您的第一个 if 语句之前。

于 2013-03-14T12:14:48.843 回答