麻烦在Java中按升序排序三个数字

时间:2013-10-23 05:35:19

标签: java sorting

我目前正在开发一个程序,但我无法让它以它应该工作的方式工作。我需要使用扫描仪输入三个整数,然后按升序输出数字。我在技术上得到了它的工作,但它没有遵循它必须遵循的结构。在我的主要方法中,我有声明和输入。然后我必须使用第二种方法进行排序。当我把排序放在这个新方法中时,我对我的生活实际上无法得到数字。它编译,我运行它,输入数字,它结束程序。没有第二种方法,它运行正常,主要是。当它在第二种方法之外运行时,我也相信我认为对数字进行排序的方式存在一些错误。

无论如何,这是我想出的代码。

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

另一件事,环顾四周我看到一些问题,人们问我与此相同(或类似)的问题,但回复坚持使用数组。我完全不能使用数组。

提前致谢。

9 个答案:

答案 0 :(得分:1)

你可以把你的号码订购的数量是3!这是3 * 2 * 1 = 6.你只有4个条件,所以你错过了这六个中的两个。试着找到其余的。

答案 1 :(得分:1)

如果您正在寻找一个简短的解决方案,也许您可​​以尝试以下方式:

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

答案 2 :(得分:0)

将它们放入列表并按Collections.sort()

对它们进行排序

答案 3 :(得分:0)

您的主要问题是您实际上并未在代码中的任何位置调用排序方法。你必须这样做并传递参数,没有神奇的仙女知道你想要称之为特定的方法。

另外,当您要求用户doubles时,为什么您的参数为ints

答案 4 :(得分:0)

在main方法中调用您的方法:

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

了解更多信息。关于在这里排序3个数字是一个老帖子:

Fastest way to sort 3 values in Java

看看你的代码,我认为通过比较它们2次来排序3个数字是不可能的。

答案 5 :(得分:0)

以下是我对此编程任务的解决方案

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


    }

}

答案 6 :(得分:0)

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

    }

}   

答案 7 :(得分:-1)

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

答案 8 :(得分:-1)

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);
    }
}
相关问题