결합 된 값이 가장 높은 3D 배열의 요소 그룹을 알아낼 방법이 있습니다. 나는 배열을 통과하는 데 사용하는 중첩 루프가 3 개 있으며 특정 조건이 충족 될 때 변수를 변경하려고합니다. 그러나 변수 중 어느 것도 사용되지 않습니다. 나는 int y
과 int m
을 바꾸기 위해 sum
이 total
을 초과하면 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);
}
편집, 난 그냥 잠시 대신 루프 내가 문제 라인으로 보이는에 범위를 벗어날 오류가있어 그것을 재구성, 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;
}
}
}
}
이것은 ... – home
제안 몇 가지 단위 테스트를 디버그 모드에서 응용 프로그램을 시작 및/또는 쓰기 할 때입니다)) 주어진 정확한 입력 3D 배열을 일을 기대 디버거에서. "sortedData [i] [j] [k]"가 "-99.99"와 같은지 확인하십시오. "합계"및/또는 "합계"가 계속 증가하는지, 어디에서 참조하십시오. – paulsm4
'sortedData'가'double [] [] []'이고 값이 부재중 인 데이터에 대해 명시 적으로'-99.99'로 설정되면 비교는 의도 한대로 작동하거나 [JVM이 완전히 망가졌습니다]. 'sortedData'가'float [] [] []'이면,'sortedData! = -99.99'는'true'로 대체 될 수 있습니다. 저는 사지에 나가서'sortedData'가'float [] [] []'라고 생각합니다. (당신은 센티넬로'-100'을 사용하거나'! = -99.99f'를 체크 할 수 있습니다.) –