为什么Math.max和Math.min无法添加或减去?

时间:2018-05-17 19:26:31

标签: java math

我想弄清楚为什么Math.max和Math.min不起作用。 我知道如果我也愿意,我可以使用if if else statment,但是如果没有别的话,还有办法吗?

    Scanner Scan = new Scanner(System.in);

    System.out.println("Enter three integers(Use enter in between): ");
    int x = Scan.nextInt();
    int y = Scan.nextInt();
    int z = Scan.nextInt();

    System.out.println(
      Math.min(x, Math.min(x, z))+", " // Prints the minimum of {x, z}
      +Math.max(x, Math.max(y, z))+", " // Prints the maximum of {x, y, z}
      +(
        Math.max(x, Math.max(y, z)) // Get the max of {x, y, z}
        +Math.min(x, Math.min(y, z)) // Get the min of {x, y, z}
        // Sum should be the sum of smallest and largest of {x, y, z}
      ) // Convert to string
      +(-x-y-z) // The sum of -1 * x+y+z, converted to String
    ); // println

1 个答案:

答案 0 :(得分:1)

Math.min(x, Math.min(x, z))更改为Math.min(x, Math.min(y, z))后,对我来说似乎没问题 Example online