int classNum = 3; //Number of classes
int stuNum = 4; //The number of students
for (int i = 1; i <= classNum; i++)
{ //Repeat 3 times
System.out.println(i + "Class");
for (int j = 1; j <= stuNum; j++)
{ //Repeat 4 times
System.out.print(j + "Please enter the grade of the person: ");
sum += input.nextInt();
int allStuNum = i * j;← This ↑ This ↓
avg = sum / allStuNum;← This ↑ This ↓
}
}
Line 12 ʻint allStuNum = i * j; , Line 13 ʻavg = sum / allStuNum;
When trying to use it outside the for block
"Variable not found" error
Maybe it's too common sense, so it's a point that isn't written in any introductory book,
If you don't know it, you will be confused when you try to reuse the variables ʻi,
j, ʻallStuNum
in the for statement like yourself.
Recommended Posts