将方法的一部分存储为另一种方法?

时间:2013-11-11 03:54:46

标签: java refactoring

基本上,我的代码在不同的场景中使用相同的几行,并且它使代码有点混乱(特别是因为我可能使我所做的过于复杂,但这是另一个问题)。我想要做的是将该段代码存储为另一个函数并在较长的函数中调用它。据我所知,哪个应该可以工作,除了更长的函数有更短的变量没有设置,如果是,我很确定它会改变函数的最终结果。

以下是较长的代码:

    public static void combat(Character a,Character b){
int battleturn = 1;
int maxTurns=20;
int draw1 = 0;
//stop after 20 turns, or stop when one player has 0 HP.
while (a.health > 0 && b.health > 0 && maxTurns > 0){
    /* run a round of combat*/
    if (b.health < 0.25 * b.maxHealth){
    if (b.getFlee(a)){
        System.out.println(">>>>>>>>>>The enemy has fled successfully<<<<<<<<<<");
        break;
    }else{
        System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
        Scanner input = new
        Scanner(System.in);
        String move = input.next();
        while(!move.equals("attack") && !move.equals("flee")){
        System.out.println("Error: Please input <attack> or <flee>.");
        input = new Scanner(System.in);
        move = input.next();
        }
        if (move.equals("attack")){
        System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " 
                   + b.getHealth() + "/" + b.getMaxHealth() + " health.");   
        System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                   a.getHealth() + "/" + a.getMaxHealth() + " health");
        maxTurns--;
        battleturn++;
        }else if(move.equals("flee")){
        if (a.getFlee(b)){
            draw1++;
            System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
            break;
        }else{
            System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " +
                       b.getHealth() + "/" + b.getMaxHealth() + " health.");         
            System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                       a.getHealth() + "/" + a.getMaxHealth() + " health");  
            maxTurns--;
            battleturn++;
        }
        }
    }

    }else{
    System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
    Scanner input = new
        Scanner(System.in);
    String move = input.next();
        while(!move.equals("attack") && !move.equals("flee")){
        System.out.println("Error: Please input <attack> or <flee>.");
        input = new Scanner(System.in);
        move = input.next();
    }
    if (move.equals("attack")){
        System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name+  "." + " Enemy has " +
                   b.getHealth() + "/" + b.getMaxHealth() + "health.");  
        System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                   a.getHealth() + "/" + a.getMaxHealth() + " health");  
        maxTurns--;
        battleturn++;
    }else if(move.equals("flee")){
        if (a.getFlee(b)){
        draw1++;
        System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
        break;
        }else{
        System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name+ "." + " Enemy has " +
                   b.getHealth() + "/" + b.getMaxHealth() + " health.");     
        System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                   a.getHealth() + "/" + a.getMaxHealth() + " health");  
        maxTurns--;
        battleturn++;
        }
    }
    }
}
}

正如您所看到的那样,有一部分代码重复了,就是这样。

System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
        Scanner input = new
        Scanner(System.in);
        String move = input.next();
        while(!move.equals("attack") && !move.equals("flee")){
        System.out.println("Error: Please input <attack> or <flee>.");
        input = new Scanner(System.in);
        move = input.next();
        }
        if (move.equals("attack")){
        System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " 
                   + b.getHealth() + "/" + b.getMaxHealth() + " health.");   
        System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                   a.getHealth() + "/" + a.getMaxHealth() + " health");
        maxTurns--;
        battleturn++;
        }else if(move.equals("flee")){
        if (a.getFlee(b)){
            draw1++;
            System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
            break;
        }else{
            System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " +
                       b.getHealth() + "/" + b.getMaxHealth() + " health.");         
            System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
                       a.getHealth() + "/" + a.getMaxHealth() + " health");  
            maxTurns--;
            battleturn++;
        }
        }
    }

如果我将那块代码设置为方法,它将无法编译,因为它没有变量battleturn,maxturns,draw1,但是如果我把它们放在那里,那么战斗的数量会变得混乱。 / p>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

Java应用程序应该是模块化的:每个类都实现自己的功能,每个方法通常执行单个操作。

在方法中,您可以使用类变量或其自己的局部变量。

如果需要多个方法使用相同的数据,它应该是类的一部分(实例和/或静态变量),或者作为参数传递给每个方法。

确保您没有在方法中定义类变量。这将创建 shadow 类变量的局部变量。

答案 1 :(得分:0)

这可能有助于您完成您想要做的事情。

private static void reportDamage(Character a,
      Character b) {
    System.out.println(a.name + " dealt "
      + a.combatRound(b) + " damage to " + b.name
      + "." + " Enemy has " + b.getHealth() + "/"
      + b.getMaxHealth() + " health.");
}

我建议改变你的战斗方法。

int battleturn = 0;
int maxTurns = 20;
// stop after 20 turns, or stop when one player has 0
// HP.
Scanner input = new Scanner(System.in);
try {
  while (a.health > 0 && b.health > 0
      && battleturn < maxturn) {
    battleturn++;
    /* run a round of combat */
    if (b.getFlee(a)) {
      System.out.println(">>>>>>>>>>"
          + "The enemy has fled successfully"
          + "<<<<<<<<<<");
      break;
    } else {
      System.out.println("Battle turn "
          + battleturn + ", <attack> or <flee>?");

      boolean isFlee = false;
      boolean isAttack = false;
      String move = input.next();
      for (;;) {
        isAttack = "attack".equalsIgnoreCase(move);
        isFlee = "flee".equalsIgnoreCase(move);
        if (isFlee || isAttack) {
          break;
        }
        System.out.println("Error: "
            + "Please input <attack> or <flee>.");
        move = input.next();
      }
      if (isAttack) {
        reportDamage(a, b);
        reportDamage(b, a);
      } else { // isFlee
        if (a.getFlee(b)) {
          System.out.println(">>>>>>>>>>"
              + "You have fled successfully"
              + "<<<<<<<<<<");
          break;
        } else {
          // b is fleeing.
          // reportDamage(a, b);
          reportDamage(b, a);
        }
      }
    }
  }
} finally {
  input.close();
}