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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏