2017-09-13 11 views
-2

私たちの割り当てでは、1つの方法しか使用できません。私はそれについて知らなかった、私は2つ書いた。だから私は、どういうわけか私の近傍条件法の機能を生命方法に統合することが可能かどうか、質問したかった。私は試しましたが、私のint近傍を初期化する方法がわかりません。次のコードを見てください:Java:これら2つのメソッドを1つにまとめるにはどうすればよいですか?

public static String[] life(String[] dish) { 
    String[] newGen = new String[dish.length]; 

    //TODO: implement this function 
    for (int line = 0; line < dish.length; line++) { // for loop going through each line 
     newGen[line] = ""; 
     for (int i = 0; i < dish[line].length(); i++) { // loops through every character in the line 
      String top = ""; // neighbours on the top 
      String middle = ""; // neighbors on the same line 
      String down = ""; // neighbors down 
      if (i == 0){ 
       if(line == 0){ 
        top = null; 
       } else { 
        top = dish[line-1].substring(i, i+2); 
       } 
       middle = dish[line].substring(i + 1, i +2); 
       if(line == dish.length -1){ 
        down = null; 
       } else { 
        down = dish[line + 1].substring(i, i + 2); 
       } 
      } else if (i == dish[line].length() - 1){ 
       if(line == 0){ 
        top = null; 
       } else { 
        top = dish[line - 1].substring(i - 1, i + 1); 
       } 
       middle = dish[line].substring(i - 1, i); 
       if(line == dish.length - 1){ 
        down = null; 
       } else { 
        down = dish [line + 1].substring(i - 1, i + 1); 
       } 
      } else { 
       if (line == 0){ 
        top = null; 
       } else { 
        top = dish[line - 1].substring(i - 1, i + 2); 
       } 
       middle = dish[line].substring(i - 1, i) + dish[line].substring(i+1, i+2); 
       if (line == dish.length - 1){ 
        down = null; 
       } else { 
        down = dish[line + 1].substring(i - 1, i + 2); 
       } 
      } 

      int neighbors = neighbourconditions(top, middle, down); 
      if (neighbors < 2 || neighbors > 3){ // neighbours < 2 or >3 neighbors -> they die 
       newGen[line] += "o"; 
      } else if (neighbors == 3){ 
       newGen[line] += "x"; // neighbours exactly 3 -> they spawn/live 
      } else { 
       newGen[line] += dish[line].charAt(i); // 2 neighbours -> stay 
      } 
     } 
    } 
    return newGen; 
} 

// helpmethod with three arguments and the conditions 
public static int neighbourconditions(String top, String middle, String down) { 
    int counter = 0; 
    if (top != null) { // if no one's on top 
     for (int x = 0; x < top.length(); ++x) { 
      if (top.charAt(x) == 'x') { 
       counter++; // count if an organism's here 
      } 
     } 
    } 
    for (int x = 0; x < middle.length(); ++x) { 
     if (middle.charAt(x) == 'x') { // two organisms, one on each side 
      counter++; // count if an organism's here 
     } 
    } 
    if (down != null) { // if no one's down 
     for (int x = 0; x < down.length(); ++x) { 
      if (down.charAt(x) == 'x') { // each neighbour down 
       counter++; // count if an organism's here 
      } 
     } 
    } 
    return counter; 
} 
+0

わからない。あなたの2つのメソッドの戻り値の型は非常に異なります。それぞれの方法の背後にある論理は何ですか? –

+0

'getNeighbors'は' life() 'で一度だけ呼び出すことができます。 'getNeighbors'のコードをコピーして、その単一の呼び出しの代わりに貼り付けるだけで問題は何ですか? – Eran

+0

'int neighbors = counter;' – fantaghirocco

答えて

3

この質問への些細な答えは、他のメソッドの本体にメソッドからコードをコピー&ペーストすることです。 IDEを使用している場合は、インラインリファクタリングツールを使用してメソッドをインライン展開できます(例:ctrl-alt-n、intellij)。

しかし、これは将来の世代にあなたの名前を呪うような行動の一種です。それは、不快で、判読不能な、維持不能なコードになります。それをしないでください。 GhostCatがコメントで指摘したように、メソッドを小さくするためには、のサイズを大きくする必要があります。

一歩を踏み出して、正しい方法で問題に近づいているかどうかを検討してください。既存のコードで繰り返しパターンを探して、単純化できるかどうかを確認します。あるいは、時には、最初に間違ったアプローチをとったことを考えると、代わりの方法を見つける必要があります。


は、私の知る限りが出て働くことができるよう、あなたがやろうとしているすべては、すぐに現在位置を囲む8つのセルでx秒の数を数えることです。

このコードをすべて実行する必要はありません。あなたは簡単に行うことができます:

for(int row = 0; row < dish.length; row++){ // each row 
    for(int col = 0; col < dish[row].length(); col++){ // each char in the row 

    int neighbors = 0; 
    for (int r = Math.max(row - 1, 0); r < Math.min(row + 2, dish.length); ++r) { 
     for (int c = Math.max(col - 1, 0); c < Math.min(col + 2, dish[row].length()); ++c) { 
     // Don't count (row, col). 
     if (r == row && c == col) continue; 

     if (dish[r].charAt(c) == 'x') ++neighbors; 
     } 
    } 

    //here ends the interesting part for you 
    if(neighbors < 2 || neighbors > 3){ 
     // etc. 

ウェイレスコード、補助的な方法の必要はありません。不必要に文字列を作成することを避けるため、より効率的です。

+0

私はあなたの助けに感謝:)。私はforループでは、括弧で皿[行] .length()と書く必要がありますif(行[r] .charAt(c)。私はあなたが何を意味するかわからない私たちはどこにでも "行"を定義していないからです。 – JavaJoker204

+0

彼らはタイプミスでしたが、修正されました –

+0

ありがとうございますが、まだ小さな問題があります。 [行] + [料理] [行] .charAt(私)、私は私が何を定義していないのですか? – JavaJoker204

4

2番目の機能の中で行うことはすべて、最初の機能で行う必要があります。だから機能1に機能2からのコードをコピー:

public static String[] life(String[] dish){ 
String[] newGen= new String[dish.length]; 

//TODO: implement this functions 
for(int row = 0; row < dish.length; row++){ // each row 
    newGen[row]= ""; 
    for(int i = 0; i < dish[row].length(); i++){ // each char in the row 
     String above = ""; // neighbors above 
     String same = ""; // neighbors in the same row 
     String below = ""; // neighbors below 
     if(i == 0){ // all the way on the left 
      // no one above if on the top row 
      // otherwise grab the neighbors from above 
      above = (row == 0) ? null : dish[row - 1].substring(i, i + 2); 
      same = dish[row].substring(i + 1, i + 2); 
      // no one below if on the bottom row 
      // otherwise grab the neighbors from below 
      below = (row == dish.length - 1) ? null : dish[row + 1].substring(i, i + 2); 
     }else if(i == dish[row].length() - 1){//right 
      // no one above if on the top row 
      // otherwise grab the neighbors from above 
      above = (row == 0) ? null : dish[row - 1].substring(i - 1, i + 1); 
      same = dish[row].substring(i - 1, i); 
      // no one below if on the bottom row 
      // otherwise grab the neighbors from below 
      below = (row == dish.length - 1) ? null : dish[row + 1].substring(i - 1, i + 1); 
     }else{ // anywhere else 
      // no one above if on the top row 
      //otherwise grab the neighbors from above 
      above = (row == 0) ? null : dish[row - 1].substring(i - 1, i + 2); 
      same = dish[row].substring(i - 1, i) + dish[row].substring(i + 1, i + 2); 
      //no one below if on the bottom row 
      //otherwise grab the neighbors from below 
      below = (row == dish.length - 1) ? null : dish[row + 1].substring(i - 1, i + 2); 
     } 

     // here is the interesting part for you: 
     int neighbors = 0; 
     if(above != null){//no one above 
      for(char x: above.toCharArray()){ //each neighbor from above 
      if(x == 'x') neighbors++; //count it if someone is here 
      } 
     } 
     for(char x: same.toCharArray()){ //two on either side 
      if(x == 'x') neighbors++;//count it if someone is here 
     } 
     if(below != null){ //no one below 
      for(char x: below.toCharArray()){//each neighbor below 
      if(x == 'x') neighbors++;//count it if someone is here 
      } 
     }; 
     //here ends the interesting part for you 
     if(neighbors < 2 || neighbors > 3){ 
      newGen[row]+= "o"; // If the amount of neighbors is < 2 or >3 neighbors -> they die 
     }else if(neighbors == 3){ 
      newGen[row]+= "x"; // If the amount of neighbors is exactly 3 neighbors -> they spawn/live 
     }else{ 
      newGen[row]+= dish[row].charAt(i); // 2 neighbors -> stay 
     } 
    } 
} 

return newGen; 

}

+3

あなたは初心者なので、*質問に答える作業の報酬をあなたに与えます。私は*本当の*答えは、(コードをさらに分割するのではなく、メソッドをマージすることによって)判読不能なコードをさらに読みにくくすることとは正反対のことをすることだと思います。 – GhostCat

関連する問題