if we have enterd three integers, let's say 1, 2, 3

how can i get the ordered form of these numbers based on their values

i tried some thing like this, but it didn't work: assuming x=3, y =2, z= 1

if(x>y && y>z)

system.out.println(x , y, z); ??????????

Also, i need to the get the square root of the averge which i already got:

i tried something like this :

double rt = Math.sqrt(average); ?????????????

can i anyone help me with this plz????

## 4 Replies - 639 Views - Last Post: 29 September 2009 - 01:46 AM

##
**Replies To:** help plz in simple program

### #2

## Re: help plz in simple program

Posted 28 September 2009 - 12:48 PM

Feed your input values into an array, then sort it. You can write your own sorting routine or you can use the Arrays.sort() method. Let me demonstrate:

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

Scanner scan = new Scanner(System.in); int[] input = new int[3]; double average = 0.0; System.out.println("Enter three integers separated by spaces. Press enter when you are finished"); for(int i = 0; i < input.length && scan.hasNextInt(); i++){ input[i] = scan.nextInt(); average += input[i]; } average /= input.length; //divide the average by the number of elements Arrays.sort(input); //remember to import java.util.Arrays to access this method

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

### #3

## Re: help plz in simple program

Posted 28 September 2009 - 01:19 PM

thanks but still i need to get the non-decreasing order of the three integers ..

i don't know if i should use if statement to compare the integers or not...

any other ideas guys ..

i don't know if i should use if statement to compare the integers or not...

any other ideas guys ..

macosxnerd101, on 28 Sep, 2009 - 11:48 AM, said:

Feed your input values into an array, then sort it. You can write your own sorting routine or you can use the Arrays.sort() method. Let me demonstrate:

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

Scanner scan = new Scanner(System.in); int[] input = new int[3]; double average = 0.0; System.out.println("Enter three integers separated by spaces. Press enter when you are finished"); for(int i = 0; i < input.length && scan.hasNextInt(); i++){ input[i] = scan.nextInt(); average += input[i]; } average /= input.length; //divide the average by the number of elements Arrays.sort(input); //remember to import java.util.Arrays to access this method

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

### #4

## Re: help plz in simple program

Posted 28 September 2009 - 01:28 PM

The Arrays.sort() method sorts the elements in ascending order. So to print the smallest, you need to access the input[0] element. Likewise for the largest element (input[2]), leaving the middle sized element at input[1]. You can use a loop to print them or access them individually.

### #5

## Re: help plz in simple program

Posted 29 September 2009 - 01:46 AM

Are you trying to sort the numbers? You can use Bubble sort or any kinds of sort...or you can take Mr. Macosxnerd101's advice, put the numbers into an array, and then use Arrays.sort() method to sort it. And I don't see anything wrong with your attempt to get the square root of the average of the three values.

Page 1 of 1