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 に答える