java,判断数组内数值为1还是0~通过前三个数值,判断后面的,不显示结果~求教!

2025-01-26 14:15:54
推荐回答(3个)
回答1:

代码如下:
public class TestArray {

public static void main(String[] args){
TestArray ta = new TestArray();
int arrayLength = 5;
String arrayB[][] = {{"1","0","1"},{"1","1","1"},{"0","0","1"},{"1","0","0"},{"1","1","0"}};
String arrayA[][] = ta.getResult( arrayB, 2);
}

private String[][] getResult(String[][] arrayB, int l){
if(l<0) return null;
String arrayA[][] = new String[arrayB.length][arrayB[0].length+l];
for(int i=0; i for(int j=0; j if(j<3){
arrayA[i][j] = arrayB[i][j];
}else{
arrayA[i][j] = this.calculator(arrayA[i][j-3], arrayA[i][j-2], arrayA[i][j-1]);
}
System.out.print("["+arrayA[i][j]+"]");
if(j == arrayA[0].length -1){
System.out.println("|");
}else{
System.out.print(",");
}
}
}
return arrayA;
}

//用于根据数组前三位
private String calculator( String i, String j, String k){
String result = "x";

if("0".equals(k)||"0".equals(j)){
return "1";
}else if("1".equals(k)&&"1".equals(j)&&"1".equals(i)){
return "0";
}

return result;
}

}
输出结果:
[1],[0],[1],[1],[x]|
[1],[1],[1],[0],[1]|
[0],[0],[1],[1],[x]|
[1],[0],[0],[1],[1]|
[1],[1],[0],[1],[1]|

回答2:

这么复杂.

回答3:

太长没看.......