gpt4 book ai didi

java - 创建一个新函数(方法),以两个 double 作为输入并返回最大的一个

转载 作者:行者123 更新时间:2023-12-02 03:00:46 25 4
gpt4 key购买 nike

我不明白如何将两个 double 作为输入并返回最大的数字。

Make a new function (method) that takes two doubles as input and return the biggest one.

我的解决方案(尝试 if 语句):

public class ex1DoubleFunction {
public static void main(String[] args) {

double a = 10;
double b = 20;
System.out.println(doublefun(a, b));

public static doublefun(a,b) {
if (a>b) {
return a;
}

else if (a<b) {
return b;
}
}

}

}

最佳答案

如果您具有同等值(value),这也可以考虑。

public static double whichGreater(double first, double second) {
if(first >= second){
return first;
} else {
return second;
}
}

关于java - 创建一个新函数(方法),以两个 double 作为输入并返回最大的一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42404515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com