hi I'm trying to sort 3 numbers from lowest to highest that the user enters...ex ...2 , 4, 6. I have a problem with finding(calculating the middle) number.

this is what i have

//create a Scanner
        Scanner scanner = new Scanner(System.in);
       

        //Prompt the user to enter three floating-point numbers
        System.out.println("Please enter three numbers:");
        double num1 = scanner.nextDouble();
        double num2 = scanner.nextDouble();
        double num3 = scanner.nextDouble();
        
                 
      //Display inputs in sorted order
        System.out.println("The inputs in sorted order are:");
        
      //Find the minimum number input by the user
        if ((num1 < num2) && (num1 < num3)) {
            System.out.println(num1);
        } else if ((num2 < num1) && (num2 < num3)) {
            System.out.println(num2);
        } else if ((num3 < num1) && (num3 < num2)) {
            System.out.println(num3);
            
        }
     //Find the middle number input by the user
        if ((num1 != num2) && (num1 != num3)) {
            System.out.println(num1);
        } else if ((num2 != num1) && (num2!= num3)) {
            System.out.println(num2);
        } else if ((num3 !=  num1) && (num3 <=  num2)) {
            System.out.println(num3);
            
       	}
        // find the maximum number input by the user
        if ((num1 > num2) && (num1 > num3)) {
            System.out.println(num1);
        } else if ((num2 > num1) && (num2 > num3)) {
            System.out.println(num2);
        } else if ((num3 > num1) && (num3 > num2)) {
            System.out.println(num3); 
      
       
        }
    }
}

display

Please enter three numbers:
2
4
6
The inputs in sorted order are:
2.0
2.0
6.0
mvmalderen commented: [ICODE]if( starlight09.postCount == 1 && starlight09.posts[0].usesCodeTags ) starlight09.rep += tux4life.posAlterPow;[/ICODE] :P +22
VernonDozier commented: Yep, code tags are good! +25

use an array and do bubble sort..like this

for(i=0;i<totalnumbers-1;i++)
{
   for(j=i+1;j<totalnumbers;j++)
   {
      if(array[i]>array[j]
      {
         temp=array[i];
        array[i]=array[j];
        array[j]=temp;
      }   
   }
 }

print array

thanks, i'll try this as well, but i got it.

u r welcome.i have no idea how u got it. But the method i have given can sort any number of numbers. If ur program is doing that..good

if (a > b) {
029 t = b;
030 b = a;
031 a = t;
032 }
033 if (a > c) {
034 t = c;
035 c = a;
036 a = t;
037 }
038 if (b > c) {
039 t = c;
040 c = b;
041 b = t;
042 }

you might also want to try my quicksort algorithm

package drawFramePackage;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class QuicksortAlgorithm {
    ArrayList<AffineTransform> affs;
    ListIterator<AffineTransform> li;
    Integer count, count2;
    /**
     * @param args
     */
    public static void main(String[] args) {
        new QuicksortAlgorithm();
    }
    public QuicksortAlgorithm(){
        count = new Integer(0);
        count2 = new Integer(1);
        affs = new ArrayList<AffineTransform>();
        for (int i = 0; i <= 128; i++){
            affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
        }
        affs = arrangeNumbers(affs);
        printNumbers();
    }
    public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
        while (list.size() > 1 && count != list.size() - 1){
            if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
                list.add(count, list.get(count2));
                list.remove(count2 + 1);
            }
            if (count2 == list.size() - 1){
                count++;
                count2 = count + 1;
            }
            else{
            count2++;
            }
        }
        return list;
    }
    public void printNumbers(){
        li = affs.listIterator();
        while (li.hasNext()){
            System.out.println(li.next());
        }
    }
}

1)The thread is 3 years old.
2)The thread is asking for simple number sorting. You are making it so comlicated with AffineTransform. It is unnecessary complicated.
3)Did you implement the Quick sort algorithm because you said "Try my quick sort algorithm" instead of "Try my quick sort algorithm implementation."
4)There are some other algorithms that are much more efficient than Quick sort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.