How to print the result of a method with System.out.println

前端 未结 5 2051
萌比男神i
萌比男神i 2021-01-17 02:34

How do I print out the result of a method? I want to print out the return of translate but it displays true or false. Suggestions please.

/**
     * @return         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-17 03:26

    Please don't forget to call the method, since you wrote it for some reason, I guess.

    System.out.println("Paperback: " + translate(pback));
    

    Now, few suggestions, do yourself a favour and change the method like below. if(pback == true), makes no sense. See, The Test of Truth, for your amusement.

    public String translate(boolean pback) {
       return pback ? "yes" : "no";
    }
    

    Well, if you don't like ternary, do this,

    public String translate(boolean pback) {
       if(pback) return "yes";
       else return "no";
    }
    

    If you like braces, put braces there,

    public String translate(boolean pback) {
       if(pback) {
          return "yes";
       } else {
          return "no";
       }
    }
    

    If you don't like 2 return statements, do this,

    public String translate(boolean pback) {
       String yesNo;
       if(pback) {
          yesNo = "yes";
       } else {
          yesNo = "no";
       }
       return yesNo;
    }
    

提交回复
热议问题