在 For 循环中更改变量

Changing a variable in Forloop

我要做一个行李统计表。 下面的代码将在条形图中每月显示 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);

我如何制作一个循环来添加代码行,并增加月份

亲切的问候

    // 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;

}

你不想在循环中改变变量,但是变量的值:

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

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]);
 }
}

您将要使用数组:

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
    }
}