gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:48:33 24 4
gpt4 key购买 nike

我有一个方法,旨在计算出 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[][][]

的地方

公共(public)类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;
}

}
}

}

最佳答案

您是否尝试过打印每个月的总和?

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

对于这一行,

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

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

要了解问题,请考虑如果您的值略有不同会发生什么情况。比如说-99.99000001。那么第一天之后,你的值(value)就已经是负数了。一个月后,总和将约为 -3099.69000031,远小于 0。由于总和始终为负数,因此它永远不会比原始总和 0 更好,因此最好的永远不会更新。

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

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

关于java - 为什么我的变量在循环中没有被改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com