-1

我创建了一个程序来使用二叉搜索树在 Java 中管理 DVD。除了 add 命令之外,我让一切都按照我想要的方式工作。当使用命令“a”添加电影时,如下所示:

a finding nemo

该程序应该检查一些条件然后添加电影。条件如下: - 如果树为空,则将电影添加到树中,默认副本为 1。 - 如果树不为空,则搜索树并将树中每部电影的标题与标题进行比较要添加到树中的电影。如果在树中找到标题,则将该电影的副本更新 1。 - 如果在树中找不到标题,只需将电影以其默认副本 1 添加到树中。

这是我必须使它工作的代码:

case 'a':
  movieToCommand = input.substring(2, input.length()).toLowerCase();

  if (movies.isEmpty()) {
    movies.add(new Dvd(movieToCommand));
    System.out.println("\"" + movieToCommand + "\"" + " has been added "
      + "to the inventory.");
  } else /*(!movies.isEmpty())*/ {
    boolean found = false;
    addIfNotEmpty(movies.returnRoot(), 1, movieToCommand, found);

    if (!found) {
      movies.add(new Dvd(movieToCommand));
      System.out.println("\"" + movieToCommand + "\"" + " has been " 
        + "added to the inventory.");
    }
  }
break;

public static void addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand, boolean found) {
    if (root == null) {
      return;
    }
    addIfNotEmpty(root.getRight(), level+1, movieToCommand, found);
    if (root.getItem().getTitle().equalsIgnoreCase(movieToCommand)) {
      root.getItem().addCopy();
      System.out.println("You have added another copy of \""
        + movieToCommand
        + "\" to the inventory.");
        found = true;
      }
    addIfNotEmpty(root.getLeft(), level+1, movieToCommand, found);
    }

我遇到的问题是:我添加电影和添加树中已经存在的电影的添加方法工作正常,除了它两次打印出这样的确认消息:(加勒比海盗已经存在于库存中并有 1 份用于测试目的)

a pirates of the caribbean

将另一个海盗副本添加到库存中。

You have added another copy of “pirates of the caribbean” to the inventory.

这是显示的第一条确认消息。

“pirates of the caribbean" has been added to the inventory.

这是显示的第二条确认消息。

为什么两条消息都被打印出来,我该如何解决?


编辑:

因此,这就是我根据对 rgettman 响应的解释将代码更改为的内容:

case 'a':
  movieToCommand = input.substring(2, input.length()).toLowerCase();

  if (movies.isEmpty()) {
    movies.add(new Dvd(movieToCommand));
    System.out.println("\"" + movieToCommand + "\"" + " has been added "
    + "to the inventory.");
  } else /*(!movies.isEmpty())*/ {
    boolean found;
    found = addIfNotEmpty(movies.returnRoot(), 1, movieToCommand);

    if (!found) {
      movies.add(new Dvd(movieToCommand));
      System.out.println("\"" + movieToCommand + "\"" + " has been " 
      + "added to the inventory.");
    }
  }
break;

public static boolean addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand) {
    boolean found = false;
    if (root == null) {
      return false;
    }
    addIfNotEmpty(root.getRight(), level+1, movieToCommand);
    if (root.getItem().getTitle().equalsIgnoreCase(movieToCommand)) {
      root.getItem().addCopy();
      System.out.println("You have added another copy of \""
      + movieToCommand
      + "\" to the inventory.");
      found = true;
    }
    addIfNotEmpty(root.getLeft(), level+1, movieToCommand);
    return found;
  }

我现在遇到的问题是:

我添加了一部电影:加勒比海盗 加勒比海盗被添加了一个副本然后添加了另一部电影:海底总动员海底总动员被添加了一个副本然后我尝试添加另一个海底总动员副本:海底总动员另一个副本被添加,但它仍然显示两个确认消息。然后我尝试添加加勒比海盗的另一个副本:加勒比海盗另一个副本被添加,并且只有一条确认消息以应有的方式显示。

    Enter a command (H for help): 
    a pirates of the caribbean
    "pirates of the caribbean" has been added to the inventory.

    Enter a command (H for help): 
    a finding nemo
    "finding nemo" has been added to the inventory.

    Enter a command (H for help): 
    a finding nemo
    You have added another copy of "finding nemo" to the inventory.
    "finding nemo" has been added to the inventory.

    Enter a command (H for help): 
    a pirates of the caribbean
    You have added another copy of "pirates of the caribbean" to the inventory.

我在编辑中做错了什么?

4

1 回答 1

1

在调用方法的情况下,您的addIfNotEmpty方法需要一个boolean参数found,您希望在调用它的方法中包含您的设置值。

但是,在 Java 中,参数是按值传递的。您正在修改 in 的副本而不是案例的变量。foundaddIfNotEmptyfound

与其修改参数,不如返回值。

public static boolean addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand) {

然后在 中声明一个局部found变量addIfNotEmpty,按照你已经在做的那样设置它,然后返回它。将方法调用的结果设置为调用方法的found.

于 2013-12-05T21:46:06.423 回答