help plz in simple program

if statment and for loop

Page 1 of 1

4 Replies - 658 Views - Last Post: 29 September 2009 - 01:46 AM Rate Topic: -----

#1 EEstar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

help plz in simple program

Post icon  Posted 28 September 2009 - 12:20 PM

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????

Is This A Good Question/Topic? 0
  • +

Replies To: help plz in simple program

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

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:

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.
Was This Post Helpful? 1

#3 EEstar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

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 ..




View Postmacosxnerd101, 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:

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.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1