gpt4 book ai didi

java - 在方法内返回 True 或 false

转载 作者:行者123 更新时间:2023-12-01 08:53:56 24 4
gpt4 key购买 nike

假设我有一个方法,我想做这个方法来返回 true 或 false。我应该声明 boolean 但我不知道如何。你能帮我吗?我该怎么做?我在我的对象中传递对象带有参数的方法,每个方法我都使用它。我想使用这个对象的名字,姓氏和金钱,如果金钱大于 50 将返回 true,如果它小于 100。在另一种情况下,此方法将返回 false。

public static int trueorfalse(String on,String surname,double money,double cost){
if(get.money>50.0 && get.cost<100.0){
System.out.println("Money is"+money);
System.out.println("Name and accepted"+on);
System.out.println("Surname and accepted"+surname);
y=true;
}
else
{
System.out.println("Money is"+money);
System.out.println("Name and denied"+on);
System.out.println("Surname and denied"+surname);
y=false;
}
return y;
}

最佳答案

将返回类型从 int 更改为 boolean。也就是说,

public static int trueorfalse(String on,String surname,double money,double cost)

public static boolean trueorfalse(String on,String surname,double money,double cost)

可以稍微简化一下,比如

public static boolean trueorfalse(String on, String surname, 
double money, double cost) {
boolean accepted = get.money > 50.0 && get.cost < 100.0;
String msg = accepted ? "accepted" : "denied";
System.out.printf("Money is %.2f%n", money);
System.out.printf("Name and %s %s%n", msg, on);
System.out.printf("Surname and %s %s%n", msg, surname);
return accepted;
}

关于java - 在方法内返回 True 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42182265/

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