LPPloveROU 2012-10-30 05:05 采纳率: 50%
浏览 2105
已采纳

怎么样让不同变量重复一个动作?

举例说明我想实现的,比如我有十个变量(整数值)以及变量的值,一个字符串。

然后代码中是判断云的数量决定天气状况:

 if (hour1cloud <= 5) {
            hour1weather = @"Clear";
        }
        if (5 < hour1cloud <= 25) {
            hour1weather = @"Mostly Clear";
        }
        if (25 < hour1cloud <= 50) {
            hour1weather = @"Partly Cloudy";
        }
        if (50 < hour1cloud <= 83) {
            hour1weather = @"Mostly Cloudy";
        }
        if (83 < hour1cloud <= 105) {
            hour1weather = @"Overcast";
        }

然后 hour2cloud, hour3cloud, hour4cloud分别对应hour2weather, hour3weather,等。当我输入hour1cloud获取hour1weather的值,能不能对所有组都变成通用方法?

  • 写回答

2条回答

  • g989_1314125 2012-10-30 05:34
    关注

    方法如下:

    - (NSString*)weatherStringFromCloud:(int)cloud {
        NSString *weather;
        if (cloud <= 5) {
            weather = @"Clear";
        } else if (cloud <= 25) {
            weather = @"Mostly Clear";
        } else if (cloud <= 50) {
            weather = @"Partly Cloudy";
        } else if (cloud <= 83) {
            weather = @"Mostly Cloudy";
        } else if (cloud <= 105) {
            weather = @"Overcast";
        } else {
            weather = nil;
        }
        return weather;
    }
    

    然后用不同变量调用:

    hour1weather = [self weatherStringFromCloud:hour1cloud];
    hour2weather = [self weatherStringFromCloud:hour2cloud];
    hour3weather = [self weatherStringFromCloud:hour3cloud];
    hour4weather = [self weatherStringFromCloud:hour4cloud];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突