Can't solve the problem when compiling | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Can't solve the problem when compiling

Hello. During compilation of one of the tasks I receive an error: java:23: error: variable declaration not allowed here Can't understand where I went wrong and how to fix it. Posted code with my comments: https://code.sololearn.com/c72Sa26qm0pd Thanks for the help

27th Sep 2017, 3:43 PM
Demetrius Tarantov
Demetrius Tarantov - avatar
4 Antworten
+ 1
You're attempting to use the variable cr prior to it being initialized and also trying to use it beyond its scope. You can't use the variable itself to initialize itself as it has no value. Also when there are no curly braces after an if statements condition Java will use the next statement only within the scope of the if statement. This means that your use of the variable cr in the print statement is out of scope as it was declared within the if block. The code below should fix these 2 issues. for (int i = 0; i < numArr.length; i++) { int cr = 0; if (numArr[i] % k) cr += numArr[i]; System.out.println("Сумма равна" + cr); }
27th Sep 2017, 6:39 PM
ChaoticDawg
ChaoticDawg - avatar
0
Yes, I understand that the problem in this place. Don't understand what is wrong here.
27th Sep 2017, 3:52 PM
Demetrius Tarantov
Demetrius Tarantov - avatar
0
Thanks for the help. with variable Declaration, I understand. Ran into another problem: error: incompatible types: int cannot be converted to boolean if (numArr[i] % k) A pointer to % I understand that inside the parentheses, both variables need to be boolean and nothing else?
2nd Oct 2017, 2:24 PM
Demetrius Tarantov
Demetrius Tarantov - avatar
0
sorry, found the error, thank you.
2nd Oct 2017, 2:34 PM
Demetrius Tarantov
Demetrius Tarantov - avatar