my first question, I'm a beginner.
Please refer to the below code, I tried to define j twice and java compiler give me an error, I totally understand.
My question here is if I cannot define a variable twice, why the "char c = ..." inside the loop is working. from the logic, when the loop body execute first time, the char c variable is defined, when the loop body execute the second time, because the char c is already defined, it should throw an error, but it didn't. Why?
public class test{
public static void main(String[] args){
int j=1;
for (int i=0; i<10; ++i){
char c = (char)(Math.random()*26+97);
System.out.println(i+1+" = "+c);
}
int j=2;
}
}
Thanks