Javaの:Doステートメントのステートメントの内側については?
質問
これは、私の他の質問<の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;
}
所属していません StackOverflow