Domanda

Questo è legato alla mia altra domanda qui

  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);
    }
  }

Quando questo metodo viene eseguito, sistema non stampa nulla. Attraverso una serie di prove di stampa, ho deciso che va all'interno del ciclo do-while. Non sono sicuro se va all'interno del ciclo for però.

È stato utile?

Soluzione

I tuoi articoli potrebbe contenere qualcosa, ma le armi potrebbe essere vuoto. Il tuo codice non sembra di fare nulla in questo caso.

Altri suggerimenti

Start from here instead:

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top