Javaの:Doステートメントのステートメントの内側については?

StackOverflow https://stackoverflow.com/questions/7829673

  •  27-10-2019
  •  | 
  •  

質問

これは、私の他の質問<のhref = "https://stackoverflow.com/questions/7829034/java-how-to-check-if-a-string-is-a-part-of-anyに関連しています-linkedlist要素 ">ここの

  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ループの内部に入るかどうかはわからない。

役に立ちましたか?

解決

あなたの項目は何かが含まれているかもしれませんが、あなたの武器は、空であってもよいです。あなたのコードは、この場合には何もしていないようです。

他のヒント

スタートここから代わります:

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top