有什么办法可以像在Java中那样打印void方法的内容
public boolean printings(String branchName){
if (findBranch(branchName)!=null){
myBranch.get(findTheBranch(branchName)).printCostumers();
return true;
}else{
System.out.println("Branch does not exist");
return false;
}
}
请注意,方法findTheBranch正常工作,而printCostumers也是一个无效方法
private int findTheBranch(String name){
for (int i=0;i<myBranch.size();i++){
if (myBranch.get(i).getName().indexOf(name)>=0){
return i;
}
}
return -1;
}
public void printCostumers(){
for(int i=0;i<myCostumer.size();i++){
System.out.println(myCostumer.get(i).getName()+":" );
for (int j=0; j<findTheCostumer(i).getTransaction().size();j++){
System.out.println(findTheCostumer(i).getTransaction().get(j));
}
}
}
其他未提及的方法正常运行。我已经在其他示例中尝试过它们。
答案 0 :(得分:2)
您包括的任何地方
System.out.println(String x);
Java将打印出x。是否在void方法中都没关系。
Java中的 void
仅指return
语句(这意味着void方法不会返回任何内容)。这并不意味着该方法不能输出任何东西(在控制台,窗口,打印机或其他地方)
还涉及您的实际代码:您的循环很可能不打印任何内容,因为它很可能会被交给一个空列表。