Let's go through the process. Because this is an int array the values are initialized to 0
. In the first for loop we assign the first int to a[0]
. Then in the second for loop we check every value in the array if it is maximal (so checking the first value against a load of zeros) then you exit the nested for loop and print the statement (with the value of a[0]
, if it was positive).
Now we go through the second cycle of the outer loop, if this was bigger again this value will be printed, otherwise the first value will be reprinted (was your array in ascending order by any chance?) and so on for each value.
As was commented, a single loop would be enough, and ensure your print statement is outside of the outer for loop
Hope this helps
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…