在For循环中更改变量

时间:2015-01-06 11:29:21

标签: java for-loop bar-chart

我必须制作行李统计图表。 下面的代码将在条形图中显示每月4个条形图。 我有一个侧边栏(这里没有给出)我可以填写我想要的细节范围(例如,1月1日至8月20日)。

现在我想到了类似于for循环的东西,它根据月数设置了一条线。 在上面的示例中,这将是以下代码

dataset.addValue(getMissing(), series1, Month1);
dataset.addValue(getMissing(), series1, Month2);
dataset.addValue(getMissing(), series1, Month3);
dataset.addValue(getMissing(), series1, Month4);
dataset.addValue(getMissing(), series1, Month5);
dataset.addValue(getMissing(), series1, Month6);
dataset.addValue(getMissing(), series1, Month7);
dataset.addValue(getMissing(), series1, Month8);

如何创建一个添加代码行的循环,并增加Month

亲切的问候

    // row keys...
    String series1 = "Luggage Lost";
    String series2 = "Customer Missing";
    String series3 = "Recovered";
    String series4 = "Forever Lost";


    // column keys...
    String Month1 = "January";
    String Month2 = "February";
    String Month3 = "March";
    String Month4 = "April";
    String Month5 = "May";
    String Month6 = "June";
    String Month7 = "July";
    String Month8 = "August";
    String Month9 = "September";
    String Month10 = "October";
    String Month11 = "November";
    String Month12 = "December";

    // create the dataset...
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();


    for (int aantalmaanden = 0; aantalmaanden==0; aantalmaanden--) {


    }

    dataset.addValue(getMissing(), series1, Month1);
    dataset.addValue(getMissing(), series1, Month2);
    dataset.addValue(getMissing(), series1, Month3);
    dataset.addValue(getMissing(), series1, Month4);
    dataset.addValue(getMissing(), series1, Month5);

    dataset.addValue(getFound(), series2, Month1);
    dataset.addValue(getFound(), series2, Month2);
    dataset.addValue(getFound(), series2, Month3);
    dataset.addValue(getFound(), series2, Month4);
    dataset.addValue(getFound(), series2, Month5);

    dataset.addValue(getHandel(), series3, Month1);
    dataset.addValue(getHandel(), series3, Month2);
    dataset.addValue(getHandel(), series3, Month3);
    dataset.addValue(getHandel(), series3, Month4);
    dataset.addValue(getHandel(), series3, Month5);

    dataset.addValue(getForeverlost(), series4, Month1);
    dataset.addValue(getForeverlost(), series4, Month2);
    dataset.addValue(getForeverlost(), series4, Month3);
    dataset.addValue(getForeverlost(), series4, Month4);
    dataset.addValue(getForeverlost(), series4, Month5);

    return dataset;

}

3 个答案:

答案 0 :(得分:0)

您不希望更改循环中的变量,而是更改变量的值:

    for (String month: new String[]{"January","February","March"...}) {
        dataset.addValue(getMissing(), series1, Month3);            
    }

答案 1 :(得分:0)

String month[] = new String[12]你必须创建几个月的数组

//在数组中添加月份

for(int i=0;i<moth.lenth;i++){
dataset.addValue(getMissing(), series1, month[i]);
}

如果你想迭代月份和系列,那么你必须使用nested for loop

String series[] = new String[4];

//在数组中添加系列

for(int i=0;i<series.lenght;i++){

 for(int j=0;j<month.lenght;j++){
   dataset.addValue(getMissing(), series[i], month[j]);
 }
}

答案 2 :(得分:0)

你想要使用数组:

String[] serieses = {
    "Luggage Lost",
    "Customer Missing",
    "Recovered",
    "Forever Lost"
};

// column keys...
String[] months = {
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

然后无聊for循环:

int seriesNum;
int monthNum;

for (seriesNum = 0; seriesNum < serieses.length; ++seriesNum) {
    for (monthNum = 0; monthNum < months.length; ++monthNum) {
        // Do something with serieses[seriesNum] and months[monthNum] here
    }
}

...或增强型for循环:

for (series : serieses) {
    for (month : months) {
        // Do something with series (a String) and month (a String) here
    }
}