2013-05-19 134 views
0

我有一种方法,它是用来计算具有最高组合值的3D数组的元素组。我有3个嵌套循环用于通过我的数组,并且在满足某些条件时我想更改这些变量。然而,没有一个变量正在被使用。如果sum超过total,我反对将int yint m更改为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[][][]

公共类的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; 
       } 

      } 
     } 

    } 
+2

这是启动应用程序在调试模式和/或写一些单元测试的时间... – home

+2

建议:通过您的代码步在调试器下。看看“sortedData [i] [j] [k]”是否等于“-99.99”。看看“总数”和/或“总数”是否增加,以及在哪里。 – paulsm4

+0

如果'sortedData'是一个'double [] [] []',并且缺省数据的值被显式设置为'-99.99',则比较将按预期工作[或者JVM被完全破坏]。如果'sortedData'是一个'float [] [] []',那么,'sortedData!= -99.99'可以替换为'true'。我正在外出,猜测'sortedData'是一个'float [] [] []'。 (你可以使用'-100'作为标记,或者检查'!= -99.99f'。) –

回答

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; 
       } 
+0

在'sortedData [i] [j] [k]!= -99.99'这一行上,NetBeans一直给我提示'invert if'。这是否可以解释发生了什么问题? 感谢您关于更新检查的提示。 – JmJ

+0

@Josh我认为这是无关的。 – Antimony

0

我在代码中看不到任何错误。

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

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

这种情况更可能总是证明是真的。 –

+0

如果它总是证明为真...那么也许总和不会大于零 – vivoconunxino

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); 
} 
相关问题