var enemyLives = Int()
var damage = Int()
enemyLives = 15
damage = 3
var attackTimesVerify = enemyLives % damage
var attackTimes = enemyLives / damage
if attackTimesVerify == 0 {
println("\(attackTimes) attacks needed!")
} else {
attackTimes = attackTimes + 1
println("\(attackTimes) attacks needed!")
}
for var i = 1 ;i <= 5; i++ {
for enemyLives; enemyLives >= 0; enemyLives -= damage {
if enemyLives > 0 {
println("round" + "\(i)" + ": " + "\(enemyLives)hp left!")
} else {
println("you win")
}
}
}
控制台输出全部是“round1”,
怎么变成有序的“round1”“round2”“round3”…… ?
我想知道你的问题现在解决了么?
你的问题是你的两个for循环的问题
for var i = 1 ;i <= 5; i++
{
enemyLives -= damage
if enemyLives >= 0
{
println("round" + "\(i)" + ": " + "\(enemyLives)hp left!")
} else
{
println("you win")
}
}
这样就好了,你可以试试
回答问题的代码语言里没有swift的选项,但是根据我的经验swift的代码用javascript基本上代码高亮没什么问题,以后代码尽量用代码语言功能标示一下,复制时也方便,大家看起来也容易读一些
试了没?可以不?