Home > Back-end > In JAVA this why cannot achieve the minimum, strives for the answer
In JAVA this why cannot achieve the minimum, strives for the answer
Time:09-27
String num. Int I, k, N. System. The out. Print (" N value is: "); C=new Scanner Scanner System. (in); N=c.n extInt (); Int [] s=new int [N]. K=s [0]; InputStreamReader isr=new InputStreamReader (System. In); BufferedReader br=new BufferedReader (isr); Try { System. The out. Print (" Input N Numbers: \ N "); for(i=0; i{ System. The out. Print (" please enter the first "+ (1 + I) +" number: "); Num=br. ReadLine (); S [I]=Integer. The parseInt (num); } for(i=0; i{ If (k> S [I]) { K=s [I]; } } System. The out. Println (" minimum value is: "+ k); } The catch (IOException e) {}
CodePudding user response:
Your sort code is as follows:
for(i=0; i{ If (k> S [I]) { K=s [I]; } }
I'll give you a change to the
K=s [0]; for(i=0; i{ If (k> S [I]) { K=s [I]; }
}
See the difference? You to assign a k s [] had nothing to do with the value of 0, if you give s all array elements are greater than zero, you won't get the s array inside the minimum value, because they are bigger than 0