为什么我的循环中的变量没有被改变?

时间:2013-05-19 18:08:50

标签: java arrays for-loop

我有一个方法可以计算出具有最高组合值的3D数组元素。我有3个嵌套循环,我用来通过我的数组,并且在满足某些条件时我想改变变量。但是没有使用任何变量。如果int y超过int m,我会将sumtotal更改为for循环的任何迭代。

感谢。这是我的代码:

    public void wettestMonth(){
        double sum = 0;
        double total = 0;
        int y = 0;
        int m = 0;

        //cycle through each year and month
        for(int i = 0; i < 34; i++){
            for(int j = 0; j < 12; j++){
                //reset the current month to 0 after each month has been cycled through
                sum = 0;
                for(int k = 0; k < 31; k++){
                    //only add the record if the current entry is not null (-99.99)
                    if(sortedData[i][j][k] != -99.99){
                        sum += sortedData[i][j][k];
                    }
                    //if the current month is wetter than the wettest one, make the current month the new wettest one
                    if(sum > total){
                        total = sum;
                        y = i;
                        m = j;
                    }
                }
            }
        }

        JOptionPane.showMessageDialog(null, "The wettest month on record was " +m +y, "Wettest Month.", JOptionPane.PLAIN_MESSAGE);

    }

编辑,我只是​​使用while循环重新构建它,而我在问题行if(sortedData[i][j][k] != -99.99)

上出现了越界错误

编辑2,这是我声明并初始化sortedData[][][]

的地方

public class GetData {

//initialises an array that holds 34 years, each with 12 months, each of which has 31 entries for reach day
public double[][][] sortedData = new double[34][12][31];

//initialises a new scanner named rainFile
private Scanner rainFile;

//method for opening the file
public void openFile() {

    try{
        //as the input for the scanner we use the rainfall file
        rainFile = new Scanner(new File("C:\\\\Users\\\\admin\\\\Documents\\\\NetBeansProjects\\\\110_term3\\\\WeatherDataFiles\\\\rainfall.txt"));
    }
    catch(Exception e){
        //if no file has been found a JOptionPane will display an error message telling the user to double-check the file path
        JOptionPane.showMessageDialog(null, "Check the file path is correct.", "No file found!", JOptionPane.ERROR_MESSAGE);
    }
}

//method for reading the file
public void readFile(){

    //ignore the first 3 lines in the data file
    String dump1 = rainFile.nextLine();
    String dump2 = rainFile.nextLine();
    String dump3 = rainFile.nextLine();

        //these nested for loops will dictate the current index of sortedData
        for(int i = 0; i < 34; i++){
            for(int j = 0; j < 12; j++){

                //ignores the year and month at the start of each line
                String dump4 = rainFile.next();
                String dump5 = rainFile.next();

                //this final nested for loop dictates the final index of sortedData
                for(int k = 0; k < 31; k++){

                    //asigns the current value of scanner rainFile to String a
                    String a = rainFile.next();

                    //converts the String a to a double type and then assigns it to the current index of sortedData
                    double dbl = Double.parseDouble(a);
                    sortedData[i][j][k] = dbl;
                }

            }
        }

    }

3 个答案:

答案 0 :(得分:3)

您是否尝试过打印每个月的金额?

最明显的可能性是,由于错误的等式检查,您的总和总是小于0。

对于这一行,

sortedData[i][j][k] != -99.99

除非值恰好是-99.99回合,否则它将是真的。这可能是无意的。例如,如果您通过某种方式通过浮点数学构造值,则由于舍入错误,您很可能无法获得完全相同的值。此外,像这样使用奇怪的哨兵值是容易出错且可读性差的。如果可以的话,最好使用像NaN这样明显的哨兵值。

要查看问题,请考虑如果您的值略有不同会发生什么。比如,-99.99000001。然后在第一天之后,您已经有了负值。一个月后,总和将大约为-3099.69000031,远小于0.因为总和总是负数,所以它永远不会比原来的总数0好,所以最好永远不会更新。

您可能还希望在日循环之外移动更新检查。这部分看起来应该使用整个月的总和,但是你运行的是每月的每一天的部分总和。只要添加的值是非负的(但它们可能不是由于前面提到的错误),它实际上不会导致不正确的结果,但你仍然应该修复它。

                if(sum > total){
                    total = sum;
                    y = i;
                    m = j;
                }

答案 1 :(得分:0)

我的代码没有看错。

也许以下情况永远不会变为真实?

if(sortedData[i][j][k] != -99.99)

答案 2 :(得分:0)

仔细检查了已经提供的第一个代码片段,在我看来,如果检查for循环内部的大小,将for循环变量移动到决赛,你一直在做,添加了一些进一步的评论。

您可以尝试通过Eclipse调试运行swing应用程序,看看您在应用程序的每一行得到的结果是什么?

如果给出正确的输入3D数组,它会发挥作用))

/**
* This method calculates the wettest month during the certain period of time.
*/
public void wettestMonth(){
    double sum = 0;
    double total = 0;
    int y = 0;
    int m = 0;
    final int numberOfYearsToCycleThrough = 34;
    final int numberOfMonthsToCycleThrough = 12;
    //cycle through each year and month
    for (int i = 0; i < numberOfYearsToCycleThrough; i++) {
        for (int j = 0; j < numberOfMonthsToCycleThrough; j++) {
            sum = 0;
            for (int k = 0; k < 31; k++){
                //only add the record if the current entry is not null (-99.99)
                if (sortedData[i][j][k] != null && sortedData[i][j][k] != -99.99) {
                   sum += sortedData[i][j][k];
                }
            }
        //if the current month is wetter than the wettest one, make the current month the new wettest one
             if (sum > total) {
                total = sum;
                y = i;
                m = j;
             }
         }
     }
     JOptionPane.showMessageDialog(null, "The wettest month on record was " +m +y, "Wettest Month.", JOptionPane.PLAIN_MESSAGE);
}