2014-02-04 2 views
-1

Эй, я получил задание идентифицировать код (а не писать самостоятельно). Я выбрал Java-рекреацию игры «Штабелер», но у меня есть некоторые проблемы с выяснением того, как работают некоторые части.Java: JButton Array, а если начальный номер

Весь код можно найти на 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]; 
      } 
    } 

Im новичок, если это помогает

+0

Что вы не понимаете в этом коде? – bobbel

+0

В основном, что он делает в глубине, я не знаю, что он точно проверяет и что он возвращает. – user3271080

+1

Хорошо, я сам не могу вам помочь, так как я не могу получить доступ к вашей ссылке в данный момент. И я не знаю, что такое 'start',' length', 'deltax' и' last'! – bobbel

ответ

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]; 
     } 
} 

Чтобы улучшить «ваш "code:

  • Избегайте использования статических переменных в ваших случаях (вместо этого используйте параметры и/или переменные класса).
  • Не используйте такие массивы, как length и deltax, если вы не используете какие-либо функциональные возможности массивов. Вы обращаетесь к ним только с array[0] и array[1]. Так почему бы не использовать, например, lengthLast/lengthCurrent и deltaxLast/deltaxCurrent?
  • Вместо go, check, draw, back, forward использовать лучшие имена методов, как runNextFloor, getNextStackLength, drawPlayground, moveStackLeft, moveStackRight.
  • Вместо if (condition == true) использование if (condition)

Существует, вероятно, намного больше, чтобы улучшить, но эй: это еще SO не Просмотр Кода!

+0

Приветствую вас, очень ценю ответ, и я обязательно улучшу свое будущее кодирование! :) – user3271080

Смежные вопросы