| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:怎样实现这样的循环?代码如下,求指教,谢谢
只看楼主 加入收藏
Jacsion
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2014-10-26
结帖率:0
收藏
已结贴  问题点数:5 回复次数:6 
怎样实现这样的循环?代码如下,求指教,谢谢
public double DWfromM() {
        if(t<=30)
        {
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=60)
        {
            int k;
            k=60-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=90)
        {
            int k;
            k=t-60;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=120)
        {
            int k;
            k=120-t;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=150)
        {
            int k;
            k=t-120;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=180)
        {
            int k;
            k=180-t;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=210)
        {
            int k;
            k=t-180;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=240)
        {
            int k;
            k=240-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=270)
        {
            int k;
            k=t-240;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=300)
        {
            int k;
            k=300-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=330)
        {
            int k;
            k=t-300;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=360)
        {
            int k;
            k=360-t;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=390)
        {
            int k;
            k=t-360;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=420)
        {
            int k;
            k=420-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=450)
        {
            int k;
            k=t-420;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=480)
        {
            int k;
            k=480-t;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=510)
        {
            int k;
            k=t-480;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=540)
        {
            int k;
            k=540-t;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=570)
        {
            int k;
            k=t-540;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=600)
        {
            int k;
            k=600-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        else if(t<=630)
        {
            int k;
            k=t-600;
            DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;
            t++;
        }
        
        return DWGeneration;
    }
搜索更多相关主题的帖子: public double 
2014-10-26 08:52
Jacsion
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2014-10-26
收藏
得分:0 
公式中的t应该是k,不好意思啊
2014-10-26 09:44
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:3 
没看到循环呀?

要是问怎么改善这段代码的话,建议把完全相同的处理放到if语句外边来做

比如把int k;提到if语句之前定义,
把DWGeneration +=(Math.exp(workedDAreaGrowthM * t) - DWGeneration;和t++;放到if语句之后做
2014-10-26 10:22
Jacsion
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2014-10-26
收藏
得分:0 
回复 3 楼 日知己所无
private int t=0;
public double DWfromM() {
        if(t<=30)
        {
            int k;
            k=t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=60)
        {
            int k;
            k=60-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=90)
        {
            int k;
            k=t-60;
           DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=120)
        {
            int k;
            k=120-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=150)
        {
            int k;
            k=t-120;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=180)
        {
            int k;
            k=180-t;
           DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=210)
        {
            int k;
            k=t-180;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=240)
        {
            int k;
            k=240-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=270)
        {
            int k;
            k=t-240;
           DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=300)
        {
            int k;
            k=300-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=330)
        {
            int k;
            k=t-300;
           DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=360)
        {
            int k;
            k=360-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=390)
        {
            int k;
            k=t-360;
             DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
            t++;
        }
        else if(t<=420)
        {
            int k;
            k=420-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=450)
        {
            int k;
            k=t-420;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=480)
        {
            int k;
            k=480-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=510)
        {
            int k;
            k=t-480;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=540)
        {
            int k;
            k=540-t;
           DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=570)
        {
            int k;
            k=t-540;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=600)
        {
            int k;
            k=600-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else if(t<=630)
        {
            int k;
            k=t-600;
             DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        t++;
        return DWGeneration;
    }
主要是想要实现计算的时候仿真时间t取0-30,计算时k取0-30,仿真时间t取31-60,计算时k取30-0,仿真时间t取61-90,计算时k取0-30,仿真时间t取91-120,计算时k取30-0,这种循环一直下去,怎么是实现?求指教,谢谢。
2014-10-26 10:40
xl881221
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:30
专家分:177
注 册:2014-3-23
收藏
得分:3 
private int t=0;
public double DWfromM() {
        int r=t/30;
        //对r判断是否为偶数
        if(r%2==0)
        {
        //为偶数k取0-30
            int k;
            k=t-30*r;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
        else
        {
        //为奇数k取30-0
            int k;
            k=30*(r+1)-t;
            DWGeneration += (Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        }
}
2014-10-26 12:05
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
先整理个容易看的,非循环的版本,然后用这个做基础,帮你分析一下

程序代码:
public class DWfromMClass {
    private int t = 0;

    public double DWfromM() {
        int DWGeneration = 0;
        int workedDAreaGrowthM = 0;
        int k = 0;
        // 仿真时间t取  0- 30,计算时k取  0-30
        // 仿真时间t取 31- 60,计算时k取 30- 0
        // 仿真时间t取 61- 90,计算时k取  0-30
        // 仿真时间t取 91-120,计算时k取 30- 0
        if (t <= 30) {          k = t;
        } else if (t <= 60) {   k = 60 - t;
        } else if (t <= 90) {   k = t - 60;
        } else if (t <= 120) {  k = 120 - t;
        } else if (t <= 150) {  k = t - 120;
        } else if (t <= 180) {  k = 180 - t;
        } else if (t <= 210) {  k = t - 180;
        } else if (t <= 240) {  k = 240 - t;
        } else if (t <= 270) {  k = t - 240;
        } else if (t <= 300) {  k = 300 - t;
        } else if (t <= 330) {  k = t - 300;
        } else if (t <= 360) {  k = 360 - t;
        } else if (t <= 390) {  k = t - 360;
        } else if (t <= 420) {  k = 420 - t;
        } else if (t <= 450) {  k = t - 420;
        } else if (t <= 480) {  k = 480 - t;
        } else if (t <= 510) {  k = t - 480;
        } else if (t <= 540) {  k = 540 - t;
        } else if (t <= 570) {  k = t - 540;
        } else if (t <= 600) {  k = 600 - t;
        } else if (t <= 630) {  k = t - 600;
        }
        DWGeneration += Math.exp(workedDAreaGrowthM * k) - DWGeneration;
        t++;
        return DWGeneration;
    }

    public static void main(String[] args) {
    }
}
2014-10-26 13:08
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
通用的公式如下:最好自己再用一些测试用例检验一下。

k = (t % 60 == 30) ? 30 :
            (t % 60 <  30) ? (t % 30):
            (t % 60 - t % 30 * 2);
2014-10-26 14:10
快速回复:怎样实现这样的循环?代码如下,求指教,谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029448 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved