使用不同的数据类型在Java中查找最多3个数字 [英] Find the max of 3 numbers in Java with different data types

查看:120
本文介绍了使用不同的数据类型在Java中查找最多3个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下三个常量:

Say I have the following three constants:

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

我想拿三个并使用 Math.max() 找到三个中的最大值,但是如果我传入了两个以上的值,那么它会给我一个错误。例如:

I want to take the three of them and use Math.max() to find the max of the three but if I pass in more then two values then it gives me an error. For instance:

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

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

Please let me know what I'm doing wrong.

推荐答案

Math.max 只接受两个参数。如果您想要最多三个,请使用 Math.max(MY_INT1,Math.max(MY_INT2,MY_DOUBLE2))

Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).

这篇关于使用不同的数据类型在Java中查找最多3个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆