[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`

  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

Impressions

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

[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
Java code that cannot be used from Kotlin (for in-house study sessions)
What is the volatile modifier for Java variables?
Why Java String typeclass comparison (==) cannot be used
Java for statement
Systemctl cannot be used on Ubuntu inside Docker container
[Java] for statement / extended for statement
[Java] It seems that `0 <hoge <10` cannot be written in the conditional expression of the ʻif` statement.
Because getSupportLoaderManager cannot be used
Books used for learning Java
Java Stream cannot be reused.
About the range and scope where Day16 variables can be used