Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同数据类型查找Java中最多3个数字_Java_Math_Max - Fatal编程技术网

使用不同数据类型查找Java中最多3个数字

使用不同数据类型查找Java中最多3个数字,java,math,max,Java,Math,Max,假设我有以下三个常数: final static int MY_INT1 = 25; final static int MY_INT2 = -10; final static double MY_DOUBLE1 = 15.5; 我想取其中的三个,然后使用Math.max()查找三个中的最大值,但如果传入两个以上的值,则会出现错误。例如: // this gives me an error double maxOfNums = Math.max(MY_INT1, MY_INT2, MY_DOUB

假设我有以下三个常数:

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;
我想取其中的三个,然后使用
Math.max()
查找三个中的最大值,但如果传入两个以上的值,则会出现错误。例如:

// this gives me an error
double maxOfNums = Math.max(MY_INT1, MY_INT2, MY_DOUBLE2);

请让我知道我做错了什么。

Math.max
只接受两个参数。如果您希望最多使用三个,请使用
Math.max(MY_INT1,Math.max(MY_INT2,MY_DOUBLE2))
如果可能,请使用Apache Commons Lang中的NumberUtils-那里有很多很棒的实用程序


如前所述,Math.max()只接受两个参数。它与当前语法不完全兼容,但您可以尝试Collections.max()

如果你不喜欢,你可以为它创建自己的方法

public class test {
    final static int MY_INT1 = 25;
    final static int MY_INT2 = -10;
    final static double MY_DOUBLE1 = 15.5;

    public static void main(String args[]) {
        double maxOfNums = multiMax(MY_INT1, MY_INT2, MY_DOUBLE1);
    }

    public static Object multiMax(Object... values) {
        Object returnValue = null;
        for (Object value : values)
            returnValue = (returnValue != null) ? ((((value instanceof Integer) ? (Integer) value
                    : (value instanceof Double) ? (Double) value
                            : (Float) value) > ((returnValue instanceof Integer) ? (Integer) returnValue
                    : (returnValue instanceof Double) ? (Double) returnValue
                            : (Float) returnValue)) ? value : returnValue)
                    : value;
        return returnValue;
    }
}
这将接受任意数量的混合数值参数(Integer、Double和Float),但返回值是一个对象,因此必须将其转换为Integer、Double或Float


它也可能抛出错误,因为没有“MY_DOUBLE2”这样的东西。

如果不使用第三方库、多次调用同一方法或创建数组,您可以找到任意数量的double的最大值,如下所示

public static double max(double... n) {
    int i = 0;
    double max = n[i];

    while (++i < n.length)
        if (n[i] > max)
            max = n[i];

    return max;
}

我有一个非常简单的想法:

 int smallest = Math.min(a, Math.min(b, Math.min(c, d)));
当然,如果你有
1000个数字
,它是不可用的,但是如果你有
3个
4个
数字,它就简单而快速

问候,,
诺伯特

如果你想做一个简单的,它会是这样的

// Fig. 6.3: MaximumFinder.java
// Programmer-declared method maximum with three double parameters.
import java.util.Scanner;

public class MaximumFinder
{
  // obtain three floating-point values and locate the maximum value
  public static void main(String[] args)
  {
    // create Scanner for input from command window
    Scanner input = new Scanner(System.in);

    // prompt for and input three floating-point values
    System.out.print(
      "Enter three floating-point values separated by spaces: ");
    double number1 = input.nextDouble(); // read first double
    double number2 = input.nextDouble(); // read second double
    double number3 = input.nextDouble(); // read third double

    // determine the maximum value
    double result = maximum(number1, number2, number3);

    // display maximum value
    System.out.println("Maximum is: " + result);
  }

  // returns the maximum of its three double parameters          
  public static double maximum(double x, double y, double z)     
  {                                                              
    double maximumValue = x; // assume x is the largest to start

    // determine whether y is greater than maximumValue         
    if (y > maximumValue)                                       
      maximumValue = y;                                        

    // determine whether z is greater than maximumValue         
    if (z > maximumValue)                                       
      maximumValue = z;                                        

    return maximumValue;                                        
  }                                                              
} // end class MaximumFinder
输出结果是这样的

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;

public static void main(String[] args) {
    double maxOfNums = max(MY_INT1, MY_INT2, MY_DOUBLE1);
}
Enter three floating-point values separated by spaces: 9.35 2.74 5.1
Maximum is: 9.35

参考资料

Java 8路。适用于多个参数:

Stream.of(first, second, third).max(Integer::compareTo).get()
您可以使用以下选项:

 Collections.max(Arrays.asList(1,2,3,4));
或者创建一个函数

public static int max(Integer... vals) {
    return Collections.max(Arrays.asList(vals)); 
}

Math.max
只接受两个参数,不多也不少

对于已经发布的答案,另一种不同的解决方案是使用:

您可以这样做:

public static void main(String[] args) {

    int x=2 , y=7, z=14;
    int max1= Math.max(x,y);

    System.out.println("Max value is: "+ Math.max(max1, z)); 
}  

没有方法的简单方法

int x = 1, y = 2, z = 3;

int biggest = x;
if (y > biggest) {
    biggest = y;
}
if (z > biggest) {
    biggest = z;
}
System.out.println(biggest);
//    System.out.println(Math.max(Math.max(x,y),z));

当涉及n个值时,必须有更好的方法。@mlissner是的,使用循环和变量
max
,检查每个变量是否大于
max
,如果是:将
max
设置为该变量。假设你的n值在一个数组中,当然。现在我自己只是一个noob,如果有人能帮我清理一下,我将不胜感激……这种方法会导致不必要的装箱/拆箱,并且包含可避免的类型检查和强制转换。最好有几个具体类型的重载方法。我相信问题是关于使用
Math.max
而不是重新创建
max
函数。我很确定问题是关于max数。。。不是最小值;)不妨使用
DoubleStream
来比较double。在这个问题中,int和double是混合的,我不认为它需要多个原语类型。。int和double。这并不能解决这个问题。但我喜欢这个前提。
double max = DoubleStream.of(firstValue, secondValue, thirdValue)
                         .max()
                         .getAsDouble();
public static void main(String[] args) {

    int x=2 , y=7, z=14;
    int max1= Math.max(x,y);

    System.out.println("Max value is: "+ Math.max(max1, z)); 
}  
int x = 1, y = 2, z = 3;

int biggest = x;
if (y > biggest) {
    biggest = y;
}
if (z > biggest) {
    biggest = z;
}
System.out.println(biggest);
//    System.out.println(Math.max(Math.max(x,y),z));