简体   繁体   English

为什么不能将Math.max和Math.min添加或减去?

[英]Why is Math.max and Math.min not able to be added or subtracted to?

I'm trying to figure out why the Math.max and Math.min aren't working. 我试图弄清楚为什么Math.max和Math.min无法正常工作。 I know I could use and if else statment if I wanted too, but is there a way to do it without if else? 我知道我可以使用,如果需要也可以使用陈述,但是有没有其他方法可以使用?

    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

一旦将Math.min(x, Math.min(x, z))更改为Math.min(x, Math.min(y, z)) ,对我来说似乎可以

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

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