如何在此for循环中正确部署gradecount计数器?

输入每个等级后,它应递增。

for (int i = 0; i < numOfGrades; i++){
    double belowzero = gradeinput.nextDouble();
    int gradecount = 1;
    System.out.println("enter grade number " + gradecount + ":" );
    gradecount++;

    while (belowzero < 0){
        belowzero = gradeinput.nextDouble();
    }
    grades[i] = belowzero;
}
}

最佳答案

在for循环外初始化等级计数。

int gradecount = 1;
for (int i = 0; i < numOfGrades; i++){
    double belowzero = gradeinput.nextDouble();

    System.out.println("enter grade number " + gradecount + ":" );
    gradecount++;

    while (belowzero < 0){
        belowzero = gradeinput.nextDouble();
    }
    grades[i] = belowzero;
}
}

07-27 13:50