gpt4 book ai didi

Java:对于 Do 语句中的语句?

转载 作者:行者123 更新时间:2023-11-29 06:08:24 25 4
gpt4 key购买 nike

这与我的另一个问题有关here

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

关于Java:对于 Do 语句中的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com