简体   繁体   English

无法按升序对 Java 中的三个数字进行排序

[英]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.我需要使用扫描仪输入三个整数,然后按升序输入 output。 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.另一件事,环顾四周,我看到有人问了一些关于我遇到的相同(或类似)问题的问题,但答复坚持使用 arrays。我根本不能为此使用数组。

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!您可以放置​​您的号码的订单数量是3! which is 3*2*1 = 6. You have only 4 conditions, so you are missing two of those six.即 3*2*1 = 6。您只有 4 个条件,因此您缺少这六个条件中的两个。 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 ?另外,当您向用户询问ints时,为什么您的参数是doubles

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 个数字进行排序是不可能的。

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.您可以使用 Math.min Math.max 来查找最小值、最大值和中值。 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创建一个列表,使用 list.add(nubmer) 添加您的号码,最后使用
Collections.sort( list ); Collections.sort(list);

import java.util.Scanner;导入 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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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