-1

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

这是更长的代码:

    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,但如果我把它们放在那里,战斗的数量就会变得混乱。

有任何想法吗?

4

2 回答 2

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();
}
于 2013-11-11T04:18:00.940 回答
0

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

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

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

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

于 2013-11-11T04:06:04.813 回答