从最小到最大排序3个数字

时间:2018-09-04 00:16:16

标签: java sorting

作为问题集的一部分,我必须按升序对3个数字进行排序。一个简单的任务,但是由于某种原因我没有得到预期的结果。不允许使用数组。下面是我的代码;我已链接到流程图here。我无法让程序对3个数字(例如5、5和-4)进行排序。当我尝试这种情况时,输出如下:

Enter three numbers.

顺序-0.04 5.0 5.0顺序5.0 -0.04 5.0

如果让那一个工作,我就无法对23、0、39进行排序。不知道我是否在这么多情况下使尝试复杂化了;我觉得我的流程图涵盖了所有可能性。预先感谢!

  import java.util.Scanner; 

class Main {
  public static void main(String[] args) {

    Scanner reader = new Scanner(System.in); 
    System.out.print("Enter three numbers.");

    double x = reader.nextDouble();
    double y = reader.nextDouble(); 
    double z = reader.nextDouble();

    if (x >= y){
            if (y >= z)
                System.out.print("In order " + z + " "+ y + " " + x);

            if  (z >= x)
                System.out.print("In order " + y + " "+ x + " " + z);

            if (x > z)
                System.out.print("In order " + y + " " + z + " " + x);
    }

    if (y > x)
    {
            if (z >= y)
                System.out.print("In order " + x + " " + y + " "+ z);
        if (z >= x)
            System.out.print("In order " + y + " " + x + " " + z);
        if (x > z)
            System.out.print("In order " + y + " " + z + " " + x);
    }


  }
}

3 个答案:

答案 0 :(得分:5)

使用<VirtualHost *:443> ProxyRequests off <Proxy *> Order deny,allow Allow from all </Proxy> <Location /> ProxyPass http://localhost:3000/ ProxyPassReverse http://localhost:3000/ </Location> SSLEngine on SSLCertificateFile "D:\xampp\apache\conf\ssl.crt\server.crt" SSLCertificateKeyFile "D:\xampp\apache\conf\ssl.key\server.key" </VirtualHost> if以及基本的加法和减法,可以不用Math.max(double, double)来解决此问题。喜欢,

Math.min(double, double)

使用double max = Math.max(x, Math.max(y, z)); double min = Math.min(x, Math.min(y, z)); double mid = x + y + z - max - min; System.out.printf("In order %f %f %f%n", min, mid, max); if比较而不是elseMath.max比较复杂。选择一个默认值,并与其他两个进行比较。喜欢,

Math.min

答案 1 :(得分:2)

如果您要坚持使用if / else逻辑,请对原始解决方案进行一些修改。注意使用else if。 我已注释掉您之前的代码行以进行比较。

import java.util.Scanner; 

class Main {
  public static void main(String[] args) {

    Scanner reader = new Scanner(System.in); 
    System.out.println("Enter three numbers.");  //Use println instead of print, that way the input begins on the next line

    double x = reader.nextDouble();
    double y = reader.nextDouble(); 
    double z = reader.nextDouble();

    if (x >= y){ //In the three responses below, y is always before x.  
            if (y >= z)
                System.out.print("In order " + z + " "+ y + " " + x);

            else if  (z >= x)
                System.out.print("In order " + y + " "+ x + " " + z);

            else if (x > z)
                System.out.print("In order " + y + " " + z + " " + x);
    }

    if (y > x){// In the three responses below, x is always before y
        if (z >= y)
            System.out.print("In order " + x + " " + y + " "+ z);
        else if (z >= x)
            //System.out.print("In order " + y + " " + x + " " + z); //In this case, z has to be smaller than y.  The order was off
            System.out.print("In order " + x + " " + z + " " + y);
        else if (x > z)
            //System.out.print("In order " + y + " " + z + " " + x);
            System.out.print("In order " + z + " " + x + " " + y); //Y is the biggest.  The order here was off.  
    }

  }
}

答案 2 :(得分:2)

您的if/else语句存在一些问题:

  1. 使用 else if 语句是因为两个条件中的任何一个都不成立,并且不是全部,因此结果将被多次打印。
  2. 第二个 if语句最后两个语句是错误的,因为如果我们输入该if语句,那么肯定(x < y),但是您在x之前打印y (现在已编辑)。

这是正确的代码:

if (x >= y) {
    if (y >= z)
        System.out.print("In order " + z + " " + y + " " + x);
    else if (z >= x)
        System.out.print("In order " + y + " " + x + " " + z);
    else if (x >= z)
        System.out.print("In order " + y + " " + z + " " + x);
} else {
    if (z >= y)
        System.out.print("In order " + x + " " + y + " " + z);
    else if (z >= x)
        System.out.print("In order " + x + " " + z + " " + y);
    else if (x >= z)
        System.out.print("In order " + z + " " + x + " " + y);
}