0

I am new to java. I am just experimenting. When I try to convert this code with javac it gives my an error

error: Cannot find symbol
  System.out.print(res);           
                   ^
symbol: variable res

Here is the full code:

class forarray {
    public static void main(String[] args) {
        int arr[][] = { { 1, 2, 3 }, { 5, 6, 7 } };
        for (int res = 0; res + arr[1][2] <= 10; res++) {
            if (res == 2) {
                res += 15;
            } else {
                res += 1;
            }
        }
        System.out.print(res);
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Tachanka
  • 133
  • 1
  • 1
  • 9
  • 2
    Look into variable scope. – Sotirios Delimanolis Nov 23 '15 at 17:05
  • You define variable res in loop but try to use it out of loop – VDanyliuk Nov 23 '15 at 17:06
  • Thank you. I resolved that issue, but now there are other issues. class test { public static void main(String[] args) { int res = 0; int arr[][] = {{1, 2, 3}, {5, 6, 7}}; for(res + arr[1][2] <= 10;res++); if(res == 2) { res += 15; }else{ res += 1; } } System.out.print(res); } } – Tachanka Nov 23 '15 at 17:14
  • Add a semicolon after "for(" to indicate the missing initializer. However, a better solution is to use a while loop instead. You are misusing the control variable of the for loop. If a for loop doesn't depend entirely on a single variable, it's usually clearer to use another control structure, such as a while loop. – Klitos Kyriacou Nov 23 '15 at 17:23
  • Thank you, I have tried with a while loop and now it works. – Tachanka Nov 23 '15 at 18:34

0 Answers0