gpt4 book ai didi

ios - 测试两个整数以确定现在是白天还是晚上

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

我正在开展一个使用国家气象局数据的项目。一个应用程序正在确定现在是白天还是晚上(来自数据 - 而不是来自用户的位置)

我的问题是:我不明白为什么我写的代码总是返回是晚上!

我有一个属性(property):

BOOL isNight;

还有一些变量:

int sunriseDifference
int sunsetDifference

表示气象站的观测时间与该气象站本地日落或日出的时间差。

我的逻辑是这样的:

sunriseDifference > 0 AND sunsetDifference > 0 然后是晚上(PM 晚上)

sunriseDifference < 0 AND sunsetDifference < 0 然后是晚上(AM 晚上)

否则就是白天。

代码如下:

if ((sunriseDifference >= 0) && (sunsetDifference >= 0)) {
self.isNight = YES;
} else if ((sunriseDifference <= 0) && (sunsetDifference <= 0)) {
self.isNight = YES;
} else self.isNight = NO;

这总是产生

self.isNight = YES

知道我的错误在哪里吗?

编辑

为了清楚起见,sunriseDifference 和 sunsetDifference 是整数,代表自日落或日出以来的分钟数。

所以在 1500 点,在加利福尼亚州的一个气象站(白天),变量的值为:

sunriseDifference = 482
sunsetDifference = -118

所以,按照我的逻辑,上面两个条件都不满足,self.isNight应该是NO。我的代码返回 self.isNight 为 YES...

我的结论是我在代码中犯了错误,而不是我的逻辑错误。有什么想法吗?

最佳答案

同意一些评论,这些评论暗示如果没有气象服务的语义,这个问题很难理解。但如果我设计一个天气服务,这两个参数总是有不同的符号:

sunriseDifference > 0 && sunsetDifference < 0  means it's day
sunriseDifference < 0 && sunsetDifference > 0 means it's night

如果我对数据含义的理解是正确的,那么代码可以更正并简化为:

self.isNight = sunriseDifference < 0 && sunsetDifference >= 0;

关于ios - 测试两个整数以确定现在是白天还是晚上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735278/

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