[英]Why aren't my variables being altered in my loops?
我有一種方法可以計算出具有最高組合值的3D陣列的元素組。 我已經使用了3個嵌套循環來遍歷數組,並且當滿足某些條件時,我想更改變量。 但是,沒有使用任何變量。 如果sum
超過total
我希望將int y
和int 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;
}
}
}
}
您是否嘗試過打印每個月的金額?
最明顯的可能性是,由於存在錯誤的相等性檢查,您的總和始終小於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;
}
我看不到您的代碼有任何問題。
也許以下條件永遠不會成立?
if(sortedData[i][j][k] != -99.99)
仔細地檢查了提供的第一個代碼段,對我來說,您似乎一直在檢查for循環內的大小,將for循環vars移到了finals,並添加了一些其他注釋。
您可以嘗試通過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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.