简体   繁体   中英

Trouble sorting three numbers in ascending order in Java

I'm currently working on a program and I just cannot get it to work the way it is supposed to work. I need to enter three integers using a scanner, and then output the numbers in ascending order. I technically got this to work but it didn't follow the structure that it HAS to follow. In my main method I have the declarations and input. Then I have to use a second method that does the sorting. I for the life of me cannot get the numbers to sort at all actually when I put the sorting inside this new method. It compiles, I run it, enter the numbers and it ends the program. Without the second method it runs correct, mostly. When it ran outside the second method there was also some errors I believe in the way that I thought was logical to sort the numbers.

Anyway this is the code that I came up with.

import java.util.Scanner;
public class Ch5PA1
{
public static void main(String[] args) {
// Declarations
Scanner input = new Scanner(System.in);

System.out.print("Enter three values: ");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
}

/** Sort Numbers */
public static void displaySortedNumbers(double num1, double num2, double num3){
if ((num1 < num2) && (num2 < num3)){
    System.out.println("The sorted numbers are " + num1 + " " + num2 + " " + num3);
    }
if ((num1 < num2) && (num2 > num3)){
    System.out.println("The sorted numbers are " + num1 + " " + num3 + " " + num2);
    }
if ((num1 > num2) && (num2 > num3)){
    System.out.println("The sorted numbers are " + num3 + " " + num2 + " " + num1);
    }
if ((num1 > num2) && (num2 < num3)){
    System.out.println("The sorted numbers are " + num2 + " " + num1 + " " + num3);
    }
}
}

Another thing, looking around I saw a few questions people asked about the same (or similar) issues I am having with this but the replies insist on using arrays. I cannot use an array at all for this.

Thanks ahead of time.

If you are looking for a short solution, perhaps you could try something like:

public static void print(final int n1, final int n2, final int n3){
    final int highest = n1 > n2 && n1 > n3 ? n1 : n2 > n1 && n2 > n3 ? n2 : n3;
    final int lowest = n1 < n2 && n1 < n3 ? n1 : n2 < n1 && n2 < n3 ? n2 : n3;
    final int middle = n1 != highest && n1 != lowest ? n1 : n2 != highest && n2 != lowest ? n2 : n3;
    System.out.printf("%d > %d > %d", highest, middle, lowest);
}

The number of orders you can put your numbers are 3! which is 3*2*1 = 6. You have only 4 conditions, so you are missing two of those six. Try to find the remaining ones.

将它们放入一个 List 并通过Collections.sort()对它们进行排序

Your main problem is that you're actually not calling your sorting method anywhere in your code. You'll have to do that and pass the parameters, there's no magical fairy that knows you want to call that particular method.

Also, why are your parameters doubles , when you're asking the user for ints ?

In your main method call your method:

public static void main(String[] args) {
    // Declarations
    Scanner input = new Scanner(System.in);

    System.out.print("Enter three values: ");
    int num1 = input.nextInt();
    int num2 = input.nextInt();
    int num3 = input.nextInt();
    displaySortedNumbers(num1, num2, num3);
}

And for more info. about sorting 3 numbers here is an old post:

Fastest way to sort 3 values in Java

And giving a look to your code, I don't think it's posible to sort 3 numbers just by comparing them 2 times.

Here is my solution to this programming task

package studies;
import java.util.Scanner;

public class SortIntegers {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println(" Enter 3 integers: ");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();

        // Sort the numbers 

        int  min, max, med;
        if( a > b ){
             if( a > c ){
              max = a;
              if( b > c ){
               med = b;
               min = c;
              }else{
               med = c;
               min = b;
              }
             }else{
              med = a;
              if( b > c ){
               max = b;
               min = c;
              }else{
               max = c;
               min = b;
              }
             }
            }else{
             if( b > c ){
              max = b;
              if( a > c ){
               med = a;
               min = c;
              }else{
               med = c;
               min = a;
              }
             }else{
              med = b;
              max = c;
              min = a;
             }
            }

        //Display results
        System.out.println("The sorted numbers are: " + min + med + max);


    }

}
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
//============================================================
/* (Sort three integers) Write a program that sorts three 
integers. The integers are entered from the input dialogs and 
stored in variables num1, num2, and num3, respectively. 
The program sorts the numbers so that 
                num1 <= num2 <= num3
 */
        int num1, num2,num3;

        System.out.println("Enter three integers to be sorted: ");
        num1 = input.nextInt();
        num2 = input.nextInt();
        num3 = input.nextInt();

        // example 1 2 5
        if ( (num1 >= num2 && num1 >= num3) && num2 >= num3 )
            System.out.printf("Sorted numbers are %d %d %d", num3, num2, num1);
        else if ((num1 >= num2 && num1 >= num3) && num3 >= num2)
            System.out.printf("Sorted numbers are %d %d %d", num2, num3, num1);
        else if ( (num2 >= num1 && num2 >= num3) && num1 >= num3 )
            System.out.printf("Sorted numbers are %d %d %d", num3, num1, num2);
        else if ((num2 >= num1 && num2 >= num3) && num3 >= num1)
            System.out.printf("Sorted numbers are %d %d %d", num1, num3, num2);
        else if ( (num3 >= num2 && num3 >= num1) && num2 >= num1 )
            System.out.printf("Sorted numbers are %d %d %d", num1, num2, num3);
        else if ((num3 >= num2 && num3 >= num1) && num1 >= num2)
            System.out.printf("Sorted numbers are %d %d %d", num2, num1, num3);

    }

}   

You might use Math.min Math.max to find min, max and median. Than print it in ascending order..

    // compute stats
    int min    = Math.min(a, Math.min(b, c));
    int max    = Math.max(a, Math.max(b, c));
    int median = a + b + c - min - max;

    // print stats
    System.out.println(min);
    System.out.println(median);
    System.out.println(max);

Create a list add your numbers by using list.add(nubmer) and finally use
Collections.sort( list );

import java.util.Scanner;

public class my_name {

public static void main(String[] args) {
    int a,b,c,l=0,s=0,m=0;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your first number:");
    a= input.nextInt();
    System.out.println("Enter your second number:");
    b= input.nextInt();
    System.out.println("Enter your third number:");
    c= input.nextInt();
    if ((a>b)&&(a>c))
    l=a;
    if((b>a)&&(b>c))
    l=b;
    if((c>a)&&(c>b))
    l=c;
    if ((a<b)&&(a<c))
    s=a;
    if ((b<a)&&(b<c))
    s=b;
    if((c<a)&&(c<b))
    s=c;
    m=(a+b+c)-(l+s);
    System.out.printf("largest number:%d ",l);
    System.out.printf("smallest number:%d ",s);
    System.out.printf("middle number:%d ",m);
    System.out.printf("The ascending order is %d %d %d",s,m,l);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM