Java:如何将这两种方法合二为一?

时间:2017-09-13 08:31:20

标签: java methods

在我们的作业中,我们只允许使用一种方法。我对此并不了解,我写了两篇。所以我想问一下,如果它能以某种方式将我的neighbourconditions方法的功能整合到生命方法中。我试过了,但我不知道如何初始化我的int邻居。请查看以下代码:

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

2 个答案:

答案 0 :(得分:4)

你在第二个功能中所做的一切都必须在第一个功能中完成。所以只需将函数2中的代码复制到函数1中:

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;

}

答案 1 :(得分: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.

方式更少的代码,不需要辅助方法。同样效率更高,因为它避免了不必要地创建字符串。