簡體   English   中英

為什么在 for 循環內部聲明有效,但在 for 循環外部聲明為全局變量無效

[英]why is declaring inside for loop works but declaring outside for loop as global variable won't work

我不知道為什么我需要在這里的第一個 for 循環中聲明計數和更正。 我最初將它們聲明為全局變量,但我在 for 循環中修改它們。 如果我不在 for 循環中聲明它們,它們將保持我認為的相同值。 我的邏輯是值在 for 循環內被改變。 我接近這個想法是錯誤的嗎?

    public static double[] gradeAllStudents(char[][] response, char[] soln) {
//      int count = 0;
        int len = soln.length;
//      int correct = 0;
        double[] result = new double[response.length];
        int index = 0;
        for(int i = 0; i < response.length; i++) {
            int count = 0;
            double correct = 0;
//          System.out.println(response[i].length);
            for(int j = 0; j < response[i].length; j++) {
                if (soln[j] == response[i][j]) {
                     correct= correct+1;
                }

                count ++;
            }
//          System.out.println(correct);
            result[index] = 100*(correct)/(response[i].length);
            index++;
            if(count != len) {
                throw new IllegalArgumentException("wrong exam");
            }

        }
        return result;
    }
}

我不確定我有沒有遇到你的問題

您可以在 for 循環外部聲明變量並在內部更改它們。

在您的示例中,代碼將如下所示

    public static double[] gradeAllStudents(char[][] response, char[] soln) {
        int count;
        int len = soln.length;
        int correct;
        double[] result = new double[response.length];
        int index = 0;
        for(int i = 0; i < response.length; i++) {
            count = 0; // restart value of count
            correct = 0; // restart value of correct
//          System.out.println(response[i].length);
            for(int j = 0; j < response[i].length; j++) {
                if (soln[j] == response[i][j]) {
                     correct= correct+1;
                }

                count ++;
            }
//          System.out.println(correct);
            result[index] = 100*(correct)/(response[i].length);
            index++;
            if(count != len) {
                throw new IllegalArgumentException("wrong exam");
            }

        }
        return result;
    }
}

PS:你可以改進你的代碼

這仍然簡單但更好

public static double[] gradeAllStudents(char[][] response, char[] soln) {
    final int len = soln.length; // this is a constant
    int correct;
    double[] result = new double[response.length];
    for (int i = 0; i < response.length; i++) {
        correct = 0;
        if (len != response[i].length) {
            throw new IllegalArgumentException("wrong exam");
        }
        for (int j = 0; j < response[i].length; j++) {
            if (soln[j] == response[i][j]) {
                correct ++;
            }
        }
        result[i] = (double)(100 * correct) / response[i].length; // casting after math operation improve the performance
    }
    return result;
}

最后歡迎來到 java :D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM