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

u5rb5r59  于 2023-10-14  发布在  Java
关注(0)|答案(6)|浏览(106)

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

Enter three numbers.

标准-0.04 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);
    }

  }
}
vawmfj5a

vawmfj5a1#

您可以使用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);

使用ifelse比较而不是Math.maxMath.min比较稍微复杂一点。选择一个默认值并与其他两个值进行比较。比如

double max = z;
if (x > max || y > max) {
    if (x > y) {
        max = x;
    } else {
        max = y;
    }
}
double min = z;
if (x < min || y < min) {
    if (x < y) {
        min = x;
    } else {
        min = y;
    }
}

double mid = x + y + z - max - min;
System.out.printf("In order %f %f %f%n", min, mid, max);
x6492ojm

x6492ojm2#

如果你想坚持使用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.  
    }

  }
}
j0pj023g

j0pj023g3#

您的if/else语句存在一些问题:
1.使用else if语句,因为其中任何一个条件都为真,而不是全部,因此结果将被多次打印。
1.第二个if语句最后两个语句是错误的,因为如果我们输入if语句,那么(x < y)肯定是正确的,但是你在y之前打印x(现在编辑)。
下面是正确的代码:

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

blpfk2vs4#

import java.util.Scanner;

 public class a9 {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Input your numbers !!");

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();

    var ok =  Math.min(a , Math.min(b , c)); 
     
    if ( ok == a ) {
        if (c > b ) {
           System.out.println("Your order is : " + "," + c + "," + b + "," + a);
        }
        else {
            System.out.println("Your order is : " + b + "," +  c + "," + a);
        }
    }
    else if (ok == b) {
        if( a > c) {
            System.out.println("Your order is : " + a + "," + c + "," + a);

        }
        else {
            System.out.println("Your order is : " + c + "," + a + "," + b);
        }
    }

    else {
        if ( b > a) {
            System.out.println("Your order is : " + a + "," +  b + "," + c);
        }
        else {
            System.out.println("Your order is : " + b + "," +  a + "," + c);
        }
    }
   

    
    
    
    
    scan.close();

}

}

ghhaqwfi

ghhaqwfi5#

我想对艾略特·弗里施最受欢迎的回答做一个更正。
首先需要检查这两个条件是否为真,这样我们就可以存储正确的“max”和“min”数字。因为在Elliot的答案中,这是不检查的,如果它们都是真的,“x”将始终是存储的数字,有时会给出给予不正确的输出。
希望它能有所帮助:)
您可以使用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);

使用ifelse比较而不是Math.maxMath.min比较稍微复杂一点。选择一个默认值并与其他两个值进行比较。比如

double max = z;
if (x > max && y > max) {
        if (x > y) {
            max = x;
        } else {
            max = y;
        }
}
if (x > max || y > max) {
    if (x > y) {
        max = x;
    } else {
        max = y;
    }
}
double min = z;
if (x < min && y < min) {
        if (x < y) {
            min = x;
        } else {
            min = y;
        }
}
if (x < min || y < min) {
    if (x < y) {
        min = x;
    } else {
        min = y;
    }
}

double mid = x + y + z - max - min;
System.out.printf("In order %f %f %f%n", min, mid, max);
cwxwcias

cwxwcias6#

DisplaySortedNumbers(x,y,z)
public static void println(int num1,num2,num3){

double temp;
    if (num1 > num2) {
        temp = num2;
        num2 = num1;
        num1 = temp;
    }
    if (num1 > num3) {
        temp = num3;
        num3 = num1;
        num1 = temp;
    }
    if (num2 > num3) {
        temp = num3;
        num3 = num2;
        num2 = temp;
    }
    System.out.printf("Sorted numbers: %s, %s, %s", num1, num2, num3);

}

相关问题