gpt4 book ai didi

java - 返回 3 个数字的最小值/绝对值

转载 作者:行者123 更新时间:2023-12-01 14:10:02 30 4
gpt4 key购买 nike

我正在做一项作业,我应该返回 3 个值中最小的一个(绝对值,如程序所述)。当只需要返回 2 个值时,它工作得很好,但是当我添加第三个值时,它开始在方法内的 Math.min 处显示“找不到符号”。 :( 我看不出问题是什么?

   public class Threeseven_RasmusDS
{
//Start of smallerAbsVal
public static int smallerAbsVal(int a, int b, int c)
{

int val = (Math.min(Math.abs(a), Math.abs(b), Math.abs(c)));
return val;
}
//End of smallerAbsVal

public static void main(String[] args)
{


int val = smallerAbsVal(6, -9, -3);
System.out.println(val);

}
//End of main

}
//End of class

最佳答案

Math.min lib 方法仅接受两个参数。如果您想要执行三个值的 min,则需要执行以下操作:

Math.min( a, Math.min(b, c) );

在您的上下文中:

int val = Math.min(Math.abs(a), Math.min(Math.abs(b), Math.abs(c)));

关于java - 返回 3 个数字的最小值/绝对值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596943/

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