简体   繁体   中英

Return True or false inside a method

Lets say i have a method and i want to do this method to return true or false.I should declare boolean but i dont know how.Could you help me with this?How can i do it?I pass objects in my methods with the parameteres and each one i use it.I want to use this objects name,surname and money if the money is bigger than 50 will return true and if it is smaller than 100.In another case this method will return 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;
}

Change the return type to boolean from int . That is,

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

to

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

You could simplify it a bit, like

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;
}

Current return-type of your method is int so your method can only return an int .

If you want your method to return a boolean type value,ie, either true or false , you need to change the return-type of your method from int to boolean

public static boolean trueorfalse(String on,String surname,double money,double cost)
{
    //content
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM