如果方法返回true,代码如何停止?

时间:2015-11-01 13:59:02

标签: java

public static void fbdeathcheck() {
    if (php <= 0) {

    }
}

private static int FBenemyhp = 10, FBenemystr = 2, FBenemydef = 2, FBenemyspd = 2, FBenemyatk = 1;

public static void firstbattle(String[] args) {
    String nextmovefirstbattle = "";
    int dodgechancefb = 0;
    while(FBenemyhp > 0 && php > 0) {
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n");
        System.out.println("-----BATTLE MODE-----");
        System.out.println("Enemy | HP " + FBenemyhp + " |");
        if (nextmovefirstbattle.equalsIgnoreCase("dodge") && dodgechancefb == 1) {
            System.out.println("DODGED!");
        }
        else {
            System.out.println("");
        }
        System.out.println("Commands:");
        System.out.println("Attack, Dodge");
        System.out.println("");
        System.out.println("You | HP " + php + " |");
        System.out.println("---------------------");
        nextmovefirstbattle = scan.nextLine();
        while (!nextmovefirstbattle.equalsIgnoreCase("attack") && !nextmovefirstbattle.equalsIgnoreCase("dodge")) {
            nextmovefirstbattle = scan.nextLine();
        }
        if (nextmovefirstbattle.equalsIgnoreCase("attack")) {
            if (FBenemyspd > pspeed){
                php = php - FBenemyatk; //Enemy attacks player
                FBenemyhp = FBenemyhp - patk; //Player attacks Enemy

            }
            else if (FBenemyspd < pspeed) {
            FBenemyhp = FBenemyhp - patk; //Player attacks enemy
            php = php - FBenemyatk; //Enemy attacks player
            }
            else {
                int chancefb = rand.nextInt(2);
                if (chancefb == 0) {
                    FBenemyhp = FBenemyhp  - patk; //Player attacks enemy
                    php = php - FBenemyatk; //Enemy attacks player

                }
                else if (chancefb == 1) {
                    php = php - FBenemyatk; //Enemy attacks player
                    FBenemyhp = FBenemyhp - patk; //Player attacks Enemy

                }
            }
        }   
        else if(nextmovefirstbattle.equalsIgnoreCase("dodge")) {
            dodgechancefb = rand.nextInt(10);
            if(dodgechancefb == 1) {
                php = php + 0;
            }
            else {
                php = php - FBenemyatk; //Enemy hits player because dodge failed.
            }
        }
    }
    php = 15;
}

基本上,如果我之后将fbdeathcheck()放在

之后
php = php - FBenemyatk;

如果fbdeathcheck()为true,我将如何使firstbattle()方法停止? fbdeathcheck()方法的目的是在健康点达到0时停止战斗。

1 个答案:

答案 0 :(得分:0)

您可以调用System.exit();来结束JVM,但这将真正结束JVM,而不会给出任何警告或结束屏幕。你应该做的是让fbdeathcheck()返回一个布尔值并让firstbattle()检查返回值并在需要时优雅地结束

public static boolean fbdeathcheck() {
    if (php <= 0) {
      return true;
    }
    return false;
}


php = php - FBenemyatk;
if (fbdeathcheck()) {
      // message battle ended
      return;
}