0

最高の組み合わせ値を持つ 3D 配列の要素のグループを解決するためのメソッドがあります。配列を通過するために使用している 3 つのネストされたループがあり、特定の条件が満たされたときに変数を変更したいと考えています。ただし、どの変数も使用されていません。私は変更int yし、それがint m上にある for ループのどのような反復にも.sumtotal

ありがとう。これが私のコードです:

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

            }
        }

    }
4

3 に答える 3

0

提供された最初のコード スニペットを慎重に確認しました。for ループ内のサイズをチェックし、for ループ変数を final に移動し、さらにコメントを追加したかどうかを確認しているように見えます。

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);
}
于 2013-05-21T19:02:08.903 に答える