0
Подскажите пожалуйста, где тут ошибка?
class A { public static int a = 0; A() { a++; } } ... A x = new A(); A y = new A(); int q ; for (int i=0;i< 5; i++) { q=(i>2)?++x.a:y.a++; System.out.print(q);
2 Respuestas
+ 1
Share the full cods with saving in playground..
How you run code without main class?
0
result is "23467" but it is not wrong
because A.a is static
x.a is same as y.a
start with a=0
constructor by create x do a++ so a=1
constructor by create y do a++ so a=2
for()
i=0; q=y.a; q=2; a++; a=3
print(q): 2
i=1; q=y.a; q=3; a++; a=4
print(q): 23
i=2; q=y.a; q=4; a++; a=5
print(q): 234
i=3; ++x.a; a=6; q=x.a; q=6;
print(q): 2346
i=4; ++x.a; a=7; q=x.a; q=7;
print(q): 23467