1

这与我的其他问题有关

  public void equipWep(String weapontoequip){
    if(items.isEmpty() && weapons.isEmpty()){
       System.out.println("You are not carrying anything.");
    } else {
      boolean weaponcheck_finished = false;
      do {
        for (String s : weapons) {
          if (s.contains(weapontoequip)) {
            System.out.println("You equip " + s + ".");
            weaponcheck_finished = true;
          } else {
            System.out.println("You cannot equip \"" + weapontoequip + "\", or you do not have it.");
            weaponcheck_finished = true;
          }
        }
      }while(weaponcheck_finished == false);
    }
  }

当此方法运行时,系统不会打印任何内容。通过一系列打印测试,我确定它进入了do-while循环内部。我不确定它是否进入for循环内部。

4

2 回答 2

2

而是从这里开始:

public void equipWithWeapon(String weapon) {
    if (items.isEmpty() && weapons.isEmpty()) {
        System.out.println("You are not carrying anything.");
        return;
    }

    String foundWeapon = findWeapon(weapon);
    if (foundWeapon == null) {
        System.out.println("You cannot equip \"" + weapon + "\", or you do not have it.");
    }

    System.out.println("You equip " + foundWeapon + ".");
}

private String findWeapon(String weapon) {
    for (String s : weapons) {
        if (s.contains(weapon)) {
            return s;
        }
    }
    return null;
}
于 2011-10-20T00:11:51.200 回答
1

你的物品可能包含一些东西,但你的武器可能是空的。在这种情况下,您的代码似乎没有做任何事情。

于 2011-10-20T00:07:45.183 回答