Below I give my decision. I think it is wrong.

public static void main(String[] args) { int [] arr = new int[] {5,10,2,4,3,7,5,3,7,4}; int X = 0; int Y = 0; for(int i = 0; i < arr.length; i++); if(i < arr[i]) if((i%2) == 0){ X = arr[i]; } } System.out.println(X); for(int i = 0; i < arr.length; i++){ if((i%2) == 1){ if(arr[i] > i) Y = arr[i]; } } System.out.println(Y); } } 
  • Use the debugger ... - Vladimir Martyanov
  • I just don’t really understand why this is why I’m asking. - Andrew
  • It would be very nice to add exactly how it happens and what you got when using the debugger. - Vladimir Martyanov
  • 10 elements for X and for Y pass and I get 5 and 7 Y correctly and X should be 3 and it turns out 5. - Andrew
  • 2
    @Andrew, when debugging, everything is simple, you step through the lines and see what values ​​the variables take, at every step you think with your head and think and draw conclusions - Cerbo

1 answer 1

Tortured person, error in several lines at once.

 int [] arr = new int[] {5,10,2,4,3,7,5,3,7,4}; int X = arr[0]; int Y = arr[1]; for(int i = 2; i < arr.length; i++) { if((i % 2) == 0) { if (arr[i] < X) { X = arr[i]; } } else { if (arr[i] > Y) { Y = arr[i]; } } 
  • let's not offend each other! - aleksandr barakin
  • Not exactly what I wanted was a task. - Andrew
  • The solution of the problem exactly fits your heading "Find the smallest number in the array under an even number and the largest under an odd number?" - ArchDemon
  • Thank you all for the advice and comments. - Andrew