我试图弄清楚如何将方法的返回值保存为Integer[]{longest, count, possible}
作为局部变量,并使用局部变量作为返回。
我遇到的一个问题是,当我编译代码时,我得到一个错误,即 int无法转换为布尔值,并且它在return语句中引用“possible”。 / p>
private Integer[] longestLength(int col, int row, boolean color)
{
// longest equals length of the longest pattern of the same color
// count equals number of the longest pattern of the same color
// possible equals number of spots, that you can play off of.
int longest = 0;
int count = 0;
int possible = 0;
//this for loop counts to 4, the patterns of the 4 possible wins
for (int i = 1; i <= 4; i++) {
//lengthOfColor saves the lengthOfColor() method to avoid calling it multiple times throughout longestLength.
Integer[] lengthOfColor = lengthOfColor(col, row, i, color);
int length = lengthOfColor[0];
//if a new longest is found, its new value is now set to itself
if (length > longest) {
longest = length;
count = 0;
possible = lengthOfColor[1];
}
//if length is the same as longest, we increase the count, and make possible equal too the larger one
if (longest != 0 && length == longest) {
count++;
possible = Math.max(lengthOfColor[1], possible);
}
}
return new Integer[]{longest, count, possible};
}
这是我的lengthOfColor方法
private Integer[] lengthOfColor(int col, int row, int pattern, boolean color) {
int x = 0;
int y = 0;
if (pattern == 1) {
// vertical pattern
y = 1;
} else if (pattern == 2) {
// horizontal pattern
x = 1;
} else if (pattern == 3) {
// diagonal slope left pattern
x = 1;
y = 1;
} else {
// diagonal slope right pattern
x = 1;
y = -1;
}
// length = how many neighbor slots are of same color
// possible equals number of slots, that you can play off of.
// whichSide = left or right if horizontal and top or bottom if vertical.
int length = 0;
int possible = 0;
Integer[] whichSide = new Integer[]{1, -1};
for (int side : whichSide) {
int i = 1;
boolean complete = false;
//while complete is false continue the loop
while (!complete) {
//mainX == horizontal pattern distance
//mainY == vertical pattern distance
int mainX = x * i * side;
int mainY = y * i * side;
//if still inbounds and if the same slot is filled and it matches the color, increment length
if (!outOfBounds(col, row, mainX, mainY) && getIsFilled(col, row, mainX, mainY) &&
checkColor(col, row, mainX, mainY) == color)
{
length++;
}
//if still inbounds and if the same slot is empty, increment possible number of spots and change complete to true
else if (!outOfBounds(col, row, mainX, mainY) && !getIsFilled(col, row, mainX, mainY) &&
getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY - row)
{
possible++;
complete = true;
}
//finish the statement to avoid a infinite loop if neither conditions are met.
else
{
complete = true;
}
// If not complete, then check one slot further.
i = i + 1;
}
}
return new Integer[] {length, possible};
}
答案 0 :(得分:0)
Integer[] longestLengthArray = new Integer[]{longest, count, possible};
return longestLengthArray;
答案 1 :(得分:-2)
最简单的解决方案:
return new Integer[]{longest, count, possible? 1 : 0};
但总的来说,创建新助手类是个好主意:
class LongestLengthRersponse{
private int longest;
private int count;
private boolean possible;
//constructor, and getters