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

前端 未结 5 2045
萌比男神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:07
    System.out.println("Paperback: " + translate(pback));
    
    0 讨论(0)
  • 2021-01-17 03:12
    public void displaybook()
        {
            System.out.println("Paperback: " + translate(pback));       
        }
    
    0 讨论(0)
  • 2021-01-17 03:13
    public String translate(boolean trueOrFalse) {       
        if(pback == true) ...
    

    Should probably be:

    public String translate(boolean trueOrFalse) {       
        if(trueOrFalse) ...
    
    0 讨论(0)
  • 2021-01-17 03:21

    It looks like you mistakenly concatenated your variable pback instead of the result of your translate method in the following statement:

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

    Instead, replace that statement with

    System.out.println("Paperback: " + translate(pback)); 
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题