+ 5

Is it correct code becz I don't get the output

https://code.sololearn.com/cdazN8ghkat1/?ref=app

21st Oct 2020, 6:43 PM
Shahedmub
5 Réponses
+ 3
I try it and it doesn't compile Edoardo Rosso put thx, I appreciate your help
21st Oct 2020, 8:03 PM
Shahedmub
+ 1
Initially after array initialization, it's values are defaultly set to 0. And by loop, for(int i: num){ //I is assigned as i= 0,next 0,......0 all 10 array elements , so you always set to num[0] to input value. Only num[0] is gets changed but not any other values by this loop by next line. So it's incorrect to use this for each loop here. num[i] = input.nextInt(); } You have also printing and comparing array 'num' instead of using 'i'...
21st Oct 2020, 8:27 PM
Jayakrishna 🇮🇳
0
No, it does not compile.
21st Oct 2020, 7:32 PM
Aleksandrs
Aleksandrs - avatar
0
You're cycling through num using an enhanced for loop with the intention of putting elements into it. What you're telling the compiler is "do this for every element in num array" when your num array size is 0. Use a regular for loop instead: for (int i = 0; i < 10; i++ ) { //enter the number in num[i] }
21st Oct 2020, 7:39 PM
Edoardo Rosso
Edoardo Rosso - avatar
0
Shahedmub You're comparing num instead of i two times in the if statement too
21st Oct 2020, 8:25 PM
Edoardo Rosso
Edoardo Rosso - avatar