【问题标题】:Stuck in While loop despite condition for loop no longer being met尽管不再满足循环条件,但仍卡在 While 循环中
【发布时间】:2020-05-18 22:37:50
【问题描述】:

我对 Swift 编码非常陌生,现在我正在尝试编写一个程序,它只需要预算和一些餐馆,然后尝试根据剩余的预算量来确定你要吃什么。似乎我的代码应该可以工作,但是由于某种原因,当它在一定次数的迭代后进入 while 循环时,它无法识别何时 totalPrice 不再大于预算。如果我在 while 循环的末尾放置一个 if 条件,验证 totalPrice 是否大于预算,它将识别它不是,然后执行我放在花括号中的任何内容,但不会离开 while 循环。我真的对出了什么问题感到困惑,并且非常感谢一些帮助理解我的代码出了什么问题。非常感谢您的帮助!

import UIKit

var prices = [30,10.99,10.5,11.7,13.99,7.99,8.99,0]
var tips = [0.20,0.18,0.18,0.18,0.18,0.15,0.15,0.0]
var restaurants = ["Salty Snow","Kerby Lane","Milto's","Trudy's","Madam Mam's","Vert's","Teji's","Home"]
var daysOfWeek = ["Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"]
let taxRate = 0.0825
var budget = 50.0

for i in 1...14{
    print("Week \(i)")
    for day in daysOfWeek {
        var randInt = Int.random(in:0..<8)
        var totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
        while totalPrice > budget {
            var randInt = Int.random(in:0..<8)
            var totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
        }
        print(totalPrice, randInt, budget)
        var place = restaurants[randInt]
        budget = budget - totalPrice
        print("\(day) meal: \(place), budget now \(budget)")

    }
    print()
    budget += 50
}

【问题讨论】:

  • 你想实现什么兄弟?从那么多循环中
  • 您在 while 循环中创建了一个新的 totalPrice 变量,因此它与您在 while 循环的条件中使用的不同。在while 循环内将var totalPrice = ... 更改为totalPrice = ...。对于这种情况,使用调试器将是一件好事。投票结束这是一个错字。
  • 是的,我是个白痴,非常感谢
  • 嗯,我们都会不时犯一些简单的错误,就像我说的那样,调试器是发现这些错误的好工具。顺便说一句,你对randInt 做同样的事情,是故意的吗?

标签: ios swift while-loop infinite-loop


【解决方案1】:

当您在 while 循环中添加 var totalPrice 而不是更新 totalPrice 时,您定义了一个具有相同名称的新变量,这就是为什么条件永远不会匹配并变成无限循环的原因。使用这个:

var prices = [30,10.99,10.5,11.7,13.99,7.99,8.99,0]
var tips = [0.20,0.18,0.18,0.18,0.18,0.15,0.15,0.0]
var restaurants = ["Salty Snow","Kerby Lane","Milto's","Trudy's","Madam Mam's","Vert's","Teji's","Home"]
var daysOfWeek = ["Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"]
let taxRate = 0.0825
var budget = 50.0

for i in 1...14{
print("Week \(i)")
for day in daysOfWeek {
    let randInt = Int.random(in:0..<8)
    var totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
    while totalPrice > budget {
        let randInt = Int.random(in:0..<8)
        totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
        print(totalPrice, randInt, budget)

    }
    let place = restaurants[randInt]
    budget = budget - totalPrice
    print("\(day) meal: \(place), budget now \(budget)")

}
print()
budget += 50
}

【讨论】:

    【解决方案2】:

    从 while 循环中的 totalPrice 变量中删除 var... 因为它不会改变而是每次都创建新变量

     while totalPrice > budget {
        var randInt = Int.random(in:0..<8)
            totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
      //var totalPrice = prices[randInt]+(prices[randInt]*taxRate)+(prices[randInt]*tips[randInt])
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多