在 Java 中按升序对三个数字进行排序时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19533567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:06:22  来源:igfitidea点击:

Trouble sorting three numbers in ascending order in Java

javasorting

提问by Austin Ray

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.

提前致谢。

采纳答案by Christian

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:

以及更多信息。关于在这里对 3 个数字进行排序是一个旧帖子:

Fastest way to sort 3 values in Java

在 Java 中对 3 个值进行排序的最快方法

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

并查看您的代码,我认为仅通过比较 2 次来对 3 个数字进行排序是不可能的。

回答by Sachin Verma

Put them into a List and sort them by Collections.sort()

将它们放入一个 List 并按以下方式排序 Collections.sort()

回答by Martijn Courteaux

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.

您可以放置​​您的号码的订单数量是3!即 3*2*1 = 6。您只有 4 个条件,因此您缺少这六个条件中的两个。尝试找到剩余的。

回答by Kayaman

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?

另外,doubles当您向用户询问时,为什么您的参数是ints

回答by Pankaj Tiwari

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

创建一个列表,使用 list.add(nubmer) 添加您的数字,最后使用
Collections.sort( list );

回答by Josh M

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);
}

回答by user6316672

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);


    }

}

回答by Seenu

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();
        Scanner input1 = new Scanner(System.in);
        System.out.println("Enter your second number:");
        b= input1.nextInt();
        Scanner input11 = new Scanner(System.in);
        System.out.println("Enter your third number:");
        c= input11.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);
    }
}

回答by Joseph Ortega

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);

    }

}