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

我与影子孤独终老i 提交于 2019-12-01 14:37:29
System.out.println("Paperback: " + translate(pback));
public void displaybook()
    {
        System.out.println("Paperback: " + translate(pback));       
    }

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;
}
public String translate(boolean trueOrFalse) {       
    if(pback == true) ...

Should probably be:

public String translate(boolean trueOrFalse) {       
    if(trueOrFalse) ...

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)); 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!