Java中的用户定义方法

发布于 2025-01-19 08:55:35 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2025-01-26 08:55:35

首先,我将删除功能中的println,并使其成为双重返回功能。

也许您要寻找的是定义简短方法的lambdas。
在这里,我们可以使用 doubleBinaryOperator

public static void main(String[] args) {       
    DoubleBinaryOperator add = (x, y) -> x + y;
    DoubleBinaryOperator sub = (x, y) -> x - y;
    DoubleBinaryOperator mul = (x, y) -> x * y;
    DoubleBinaryOperator div = (x, y) -> x / y;
    double num1 = 5;
    double num2 = 10;
    System.out.printf("sum %f%nsub %f%nmul %f%ndiv %f%n",
        add.applyAsDouble(num1, num2),
        sub.applyAsDouble(num1 + num2, num1),
        mul.applyAsDouble(num1, num2),
        div.applyAsDouble(num1 + num2, (num1 + num2) - num1));
}

不幸的是.applyasdouble有点尴尬。 applyasdouble应解释为: apply 两个(double)参数,并将结果作为double 值。

您的解决方案也很好。

First I would remove the println inside the function and make it a double returning function.

Maybe lambdas to define short methods is what you are looking for.
Here we can use DoubleBinaryOperator.

public static void main(String[] args) {       
    DoubleBinaryOperator add = (x, y) -> x + y;
    DoubleBinaryOperator sub = (x, y) -> x - y;
    DoubleBinaryOperator mul = (x, y) -> x * y;
    DoubleBinaryOperator div = (x, y) -> x / y;
    double num1 = 5;
    double num2 = 10;
    System.out.printf("sum %f%nsub %f%nmul %f%ndiv %f%n",
        add.applyAsDouble(num1, num2),
        sub.applyAsDouble(num1 + num2, num1),
        mul.applyAsDouble(num1, num2),
        div.applyAsDouble(num1 + num2, (num1 + num2) - num1));
}

Unfortunately .applyAsDouble is a bit awkward. applyAsDouble should be interpreted as: apply two (double) arguments and get the result as double value.

Your solution is fine too.

柳絮泡泡 2025-01-26 08:55:35
public static void main(String[] args) {
    double num1 = 5;
    double num2 = 10;
    BiConsumer<Double, Double> add = (a, b) -> System.out.println("Sum is: " + (a + b));
    BiConsumer<Double, Double> sub = (a, b) -> System.out.println("Difference is: " + ((a + b) - a));
    BiConsumer<Double, Double> mul = (a, b) -> System.out.println("Multiplication is: " + (a * b));
    BiConsumer<Double, Double> div = (a, b) -> System.out.println("Division is: " + ((a + b) / ((a + b) - a)));
    Stream.of(add, sub, mul, div).forEach(x -> x.accept(num1, num2));
}

您可以这样写。 :)

public static void main(String[] args) {
    double num1 = 5;
    double num2 = 10;
    BiConsumer<Double, Double> add = (a, b) -> System.out.println("Sum is: " + (a + b));
    BiConsumer<Double, Double> sub = (a, b) -> System.out.println("Difference is: " + ((a + b) - a));
    BiConsumer<Double, Double> mul = (a, b) -> System.out.println("Multiplication is: " + (a * b));
    BiConsumer<Double, Double> div = (a, b) -> System.out.println("Division is: " + ((a + b) / ((a + b) - a)));
    Stream.of(add, sub, mul, div).forEach(x -> x.accept(num1, num2));
}

you could write it like this. :)

天生の放荡 2025-01-26 08:55:35
    double num1 = 5;
    double num2 = 10;
    double sum=sum(num1,num2);
    double sub=sub(sum,num1);
    double mul=mul(num1,num2);
    double div=div(sum,sum-1);
    System.out.println("sum is: "+sum+"\n Difference is: "+sub+"\nMultiplication is: "+mul+"\nDivision is: "+div);
    sum(num1, num2);
    sub(num1 + num2, num1);
    mul(num1, num2);
    div(num1 + num2, (num1 + num2) - num1);
}

public static double sum(double num1, double num2)
{
    return num1+num2;
}

public static double sub(double sumVal, double num1)
{
    return sumVal-num1;
}

public static double mul(double num1, double num2)
{
    return num1*num2;
}

public static double div(double sumVal, double subVal)
{
    return sumVal/subVal;
}
    double num1 = 5;
    double num2 = 10;
    double sum=sum(num1,num2);
    double sub=sub(sum,num1);
    double mul=mul(num1,num2);
    double div=div(sum,sum-1);
    System.out.println("sum is: "+sum+"\n Difference is: "+sub+"\nMultiplication is: "+mul+"\nDivision is: "+div);
    sum(num1, num2);
    sub(num1 + num2, num1);
    mul(num1, num2);
    div(num1 + num2, (num1 + num2) - num1);
}

public static double sum(double num1, double num2)
{
    return num1+num2;
}

public static double sub(double sumVal, double num1)
{
    return sumVal-num1;
}

public static double mul(double num1, double num2)
{
    return num1*num2;
}

public static double div(double sumVal, double subVal)
{
    return sumVal/subVal;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文