繁体   English   中英

Swift function 返回错误 output

[英]Swift function returning wrong output

我创建了一个 function 打印学生在哪里 - 课,长时间休息或短暂休息(以及哪个短暂休息)

Function参数为:

    shortBreak - duration of short break
    longBreak - duration of long break
    lesson - after which lesson is long break
    time - the time where students are located

我测试 function 的参数是

   currently(shortBreak: 5, longBreak: 15, lesson: 3, time: "10:37)

所以应该是

  8:00 - 8:45 - lesson
  8:45 - 8:50 - short break
  8:50 - 9:35 - lesson
  9:35 - 9:40 - short break
  9:40 - 10:25 - lesson
  10:25 - 10:40 - long break (after 3.lesson is long break)
  

这意味着学生们目前正在长时间休息,但我的 output 说他们正在上课。

我再次查看了代码,但我找不到我错的地方。

另外,有没有更好的方法来操纵时间参数?

import UIKit

enum current {

case lesson
case shortBreak
case longBreak

}

func currently(shortBreak: Int, longBreak: Int, lesson: Int, time:String){

    var shortBreakCounter: Int = 0
    var currentLesson: Int = 1
    var currentHours: Int = 8
    var currentMinute: Int = 0
    var currentCase: current = .lesson


    let minute: Int = Int(time.suffix(2)) ?? 0
    let hour: Int = Int(time.prefix(2)) ?? 0

    if(checkValid(hour: hour, minute: minute)){
    
        print("Invalid time")
    }

    else {
  
        while isInRange(hour: currentHours, minute: currentMinute) {
    
            currentCase = .lesson
    
            currentMinute += 45
    
            if currentMinute >= 60 {
                currentHours += 1
                currentMinute = 0
            }
    
            if currentLesson == lesson {
            
                currentCase = .longBreak
            
                currentMinute += longBreak
            
                if currentMinute >= 60 {
                    currentHours += 1
                    currentMinute = 0
                }
            
            } else{
              
                currentCase = .shortBreak
            
                currentMinute += shortBreak
            
                shortBreakCounter += 1
            
                if currentMinute >= 60 {
                    currentHours += 1
                    currentMinute = 0
                }
            
            }
        
            currentLesson += 1
    
        }

        switch currentCase {
    
            case .shortBreak:
                print("Students are on \(shortBreakCounter) short break")
            case .longBreak:
                print("Students are on long break")
            default:
                print("Students are on lesson")
            }
  
    }

}

func checkValid(hour: Int, minute:Int) -> Bool {

    if hour >= 16 || hour < 8 {
    
        return true
    
    } else if minute > 59 || minute < 0 {
     
        return true
    }

    return false

}

func isInRange(hour: Int, minute: Int) -> Bool{

    if 8..<16 ~= hour &&  0..<60 ~= minute  {
    
        return false

    }

        return true

    }

    currently(shortBreak: 5, longBreak: 15, lesson: 3, time: "10:37")
if currentMinute >= 60 {
    currentHours += 1
    currentMinute = 0
}

这是错误的,您应该将currentMinute设置为当前值 - 60。

想象一下,如果值为 75,则有 15 分钟过去了,因为您将其设置为 0。

它应该是

if currentMinute >= 60 {
    currentHours += 1
    currentMinute = currentMinute - 60
}

如果 checkValid 为真,isInRange 总是返回假。 您总是得到默认的 currentCase(课程)。

“编辑时间参数”的更好方法是使用日期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM