Hi,
I know I probably wen the long way to resolve this, but I am a novice. please take a look at my code and let know whats wrong with it. I am failing at the last requirement which is "The displayed numbers should be sorted in ascending order" and they are. I noted as much as i code to make sense of my code. Thank you in advance
package com.codegym.task.task06.task0622;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Ascending numbers
*/
public class Solution {
public static void main(String[] args) throws Exception {
/*write your code here.... create keyboard input, create an array of 5 called list-
input 5 numbers from keyboad*/
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
int[] list = new int[5];
for (int i = 0 ; i<list.length;i++){
list[i]=Integer.parseInt(rd.readLine());
}
for(int i = 0; i<list.length; i++){
/* loop through list from top to bottom and replace each iteration with the smallest
number*/
int y = MinPos(i, list);// call MinPos method get the minimum number's index
int x = list[i]; //currnt number, with index i stored in variable x in order to preserve.
list[i]=list[y];//current number moved to the index position y which is the minimum number
list[y]=x;// the minimum number is moved to current number index.
}
//print newlyy ordered list
for(int i = 0 ; i<list.length; i++){
System.out.println(list[i]);
}
}
public static int MinPos(int x, int[] list){
//find the smallest number in the list starting from index x, and retun it's index number;
int minpos = x;
int minnumber= list[x];
for( int i= 0 ;i < list.length - x; i++){
if(list[x] < list[minpos]){
minnumber=list[minpos];
minpos=x;
}
x++;
}
return minpos;
}
}