Java:JButton Array以及初学者问题

时间:2014-02-04 13:49:54

标签: java arrays if-statement jbutton

嘿,我得到了识别代码的任务(不是我自己写的)。我选择了游戏“Stacker”的Java娱乐,但是我在查找某些部分如何工作时遇到了一些麻烦。

整个代码可以在This site

找到

这部分是主要问题,如果有人能详细解释,我会很高兴。

        public int check(){
            if (start == true){
                    return length[1];
            } else if (last<deltax[1]){
                    if (deltax[1]+length[1]-1 <= last+length[0]-1){
                            return length[1];
                    } else {
                            return length[1]-Math.abs((deltax[1]+length[1](last+length[0]));
                    }
            } else if (last>deltax[1]){
                    return length[1]-Math.abs(deltax[1]-last);
            } else {
                    return length[1];
            }
    }

如果有帮助我是初学者

1 个答案:

答案 0 :(得分:0)

除了事实,整个代码是如此混乱,我会向你解释:

// checks, how long will be the next stack line and returns it (values from 0 to 5)
public int check() {
        // if this is the first line for the player
        if (start == true) {
                // return the size of the current stack
                // (in this case it is 5, because initialization of array length is {5, 5})
                return length[1];
        // if the horizontal index of the last stack is smaller than the horizontal index of the current stack
        } else if (last < deltax[1]) {
                // if the horizontal index of the current stack plus the current stack size is less or equal than the horizontal index of the last stack plus the last stack size
                if (deltax[1] + length[1] <= last + length[0]) {  // i removed the double -1, it's unnecessary
                        // return the size of the current stack
                        return length[1];
                } else {
                        // else return the congruent size of the current and the last stack
                        return length[1] - Math.abs((deltax[1] + length[1]) - (last + length[0]));
                }
        // if the horizontal index of the last stack is greater than the horizontal index of the current stack
        } else if (last > deltax[1]) {
                // return the congruent size of the current stack minus the difference between the current and the last horizontal index of the stack
                return length[1] - Math.abs(deltax[1] - last);
        // else the last stack will be directly under the currect stack
        } else {
                // so just return the size of the current stack
                return length[1];
        }
}

改善“你的”代码:

  • 避免在您的情况下使用静态变量(改为使用参数和/或类变量)。
  • 如果您没有使用任何数组功能,请不要使用lengthdeltax等数组。您只能使用array[0]array[1]访问它们。那么为什么不使用例如lengthLast / lengthCurrentdeltaxLast / deltaxCurrent
  • 取代gocheckdrawbackforward使用更好的方法名称,例如runNextFloorgetNextStackLengthdrawPlaygroundmoveStackLeftmoveStackRight
  • 而不是if (condition == true)使用if (condition)

可能还有很多需要改进的地方,但是嘿:这仍然是 SO 而不是 CodeReview