【发布时间】:2015-07-20 14:13:38
【问题描述】:
我尝试返回 correctPiece 和 correctDest 但是
return correctPiece;
加下划线并出现错误“无法访问代码”。
如何同时退货?
while(correctPiece && !correctDest) {
System.out.println("Click on a destination");
toXCo = s.getToXInt();
toYCo = s.getToYInt();
Move found = null;
for( Move m : moves){
//checks if move can be done
if (m.ToX() == toXCo && m.ToY() == toYCo){
//if move is allowed- exit loop
found = m;
correctDest = true;
}
}
if (found == null) {
//if move can't be, ask for new co-ordinates
System.out.println("This move is not legal \n");
correctDest = false;
correctPiece = false;
}
return correctDest;
return correctPiece;
}
【问题讨论】:
-
你不能从一个函数返回多个值。为确保这两个值都反映到您调用的位置,请通过引用传递它们。
标签: java while-loop boolean return