问题描述
伙计们,我编写了这段小代码来模拟神奇宝贝的捕获过程,共有三种类型的oke球和两种浆果。我想重复使用浆果来降低神奇宝贝的kp,以便在下降后用足够坚固的pokeball捕捉它。 该功能正在启动,但是重复循环没有停止!宠物小精灵被捉住了,但并没有停下来。有人可以帮助我使此代码正常工作吗?预先感谢!
struct Pokeball {
var name: String
var power: Int
}
var pokeball = Pokeball(name: "Pokeball",power: 5)
var superball = Pokeball(name: "superball",power: 7)
var hyperball = Pokeball(name: "Hyperball",power: 10)
struct Berries {
var name: String
var power: Int
}
var himmihBerry = Berries(name: "himmihBerry",power: 1)
var goldenBerry = Berries(name: "golden Berry",power: 3)
struct Pokemon {
var name: String
var wp: Int
var kp: Int
}
var glumanda = Pokemon(name: "glumanda",wp: 10,kp: 6)
var dialga = Pokemon(name: "Dialga",wp: 30,kp: 25)
struct Poketrainer {
var name: String
var balls: Int
var berries: Int
var pokemons: [Pokemon]
mutating func Catching(pokemonToCatch: Pokemon,usingBall: Pokeball,usedBerry: Berries) {
var pokemonToCatch = pokemonToCatch
repeat {
print("\(name) is using \(usedBerry.name)...")
pokemonToCatch.kp -= usedBerry.power
berries -= 1
print("\(name) is throwing a \(usingBall.name) to catch \(pokemonToCatch.name)")
if usingBall.power > pokemonToCatch.kp {
pokemons.append(pokemonToCatch)
print("\(pokemonToCatch.name) was caught!")
balls -= 1
} else {
print("Pokemon was to strong...")
}
} while balls > 0 || pokemons.count < 0
print("Hunt is over...")
}
}
var ash = Poketrainer(name: "Ash",balls: 3,berries: 5,pokemons: [])
ash.Catching(pokemonToCatch: glumanda,usingBall: superball,usedBerry: himmihBerry)
print("balls: \(ash.balls),pokemon: \(ash.pokemons.count)")
解决方法
您可能想在balls -= 1
之后执行print("Pokemon was too strong")
。我认为无论您是否捉到神奇宝贝,皮球都被消耗掉了