首页 > 解决方案 > Eclipse 控制台在运行程序的过程中会自行清除的一些原因是什么?

问题描述

我正在 Eclipse 中制作 RPG(基于文本的)游戏。

我尝试使用“System.err.println”调试我的代码,但是,控制台没有显示任何内容,并且似乎处于无限循环打印新行(“\n”)

这是到目前为止我必须运行我的游戏的循环:

while(Health > 0) {
    if(!ENEMY) {
    output("\n");
    ERRoutput("CHECK1");
    encounterEnemy(classBase.lvl);
    }
    move();
    if(Health > MAX_HEALTH) {
     Health = MAX_HEALTH;
     output("\n");
     ERRoutput("CHECK2");
    }
    if(!ENEMY && classBase.XP >= classBase.neededXP){ 
        classBase.lvlUp();
        output("\n");
    }
    if(classBase.LVLUP()) {
     output("\n");
     ERRoutput("CHECK3");
     distributePoints();
     if(Strength < 2) Strength = 2;
     }
}

这也是唯一使用“\n”的位置(创建新行)

我希望代码的输出是:

  1. 如果敌人尚未生成,则生成敌人
  2. 提示用户杀死敌人直到敌人死亡(移动方法)
  3. 检查是否获得了足够的 XP(经验)来升级
  4. 重启循环

实际发生的情况:

  1. 生成敌人
  2. 提示用户杀死敌人直到敌人死亡(移动方法)
  3. 检查是否获得了足够的 XP(经验)来升级
  4. 生成敌人
  5. 控制台自行清除并陷入无限循环

控制台根本没有输出

对于询问“敌人”分配在哪里的人:

static boolean ENEMY = false;
public static void encounterEnemy(int lvl) {
    eHealth = 20;
    eHealth *= lvl + 1;
    eStamina *= lvl;
    eStamina *= lvl;
    eStrength *= lvl;
    eSpeed *= lvl;
    eSpeed /= 2;
    eDamage = 8;
    eDamage *= lvl;
    if(lvl > 7) {
        eDamage /= 2;
    }

    ENEMY = true;
    output("A LVL" + lvl + " enemy has appeared!");
    showEnemyHealth();
}

它在“encounterEnemy”中分配 (当敌人被杀死时,它在“move()”中再次设置为false)

升级代码:

public static void lvlUp() {
    upgrade = true;
    lvl++;
    Main.output(" _ _ _ _ _ _ _ _");
    Main.output("|               |");
    Main.output("|    LVL UP     |");
    Main.output("|  LEVEL: " + lvl + "     |");
    Main.output("|  HEALTH:+25   |");
    Main.output("|  STAMINA:+15  |");
    Main.output("|  STEALTH:+5   |");
    Main.output("|  STRENGTH:+15 |");
    Main.output("|  SPEED:+2     |");
    Main.output("| _ _ _ _ _ _ _ |");

    Main.Health += 25;
    Main.Stamina += 15;
    Main.Stealth += 5;
    Main.Strength += 15;
    Main.Speed += 2;

    neededXP = 35*lvl;
    XP = 0;
}

对于询问所有大写字母的升级方法是什么的人:

public static boolean LVLUP() {
    if(upgrade) {
        upgrade = false;
        Main.points += 2;
        return true;
    } else {
        upgrade = false;
        return false;
    }

}

最后但并非最不重要的一点是 move 方法:

static boolean proceed;
static boolean Continue;
public static void move() {
    while(Health > 0 && eHealth > 0 && Health > 0) {
        showStats();
        proceed = true;
        Continue = true;
        output("What do you do?");
        if(classBase.dmg_Spls == true) {
            output("[10] Damage enemy with spell (Damage: " + (7.5*classBase.lvl) + ")");
        }
        if(classBase.heal_Spls == true) {
            output("[2] Heal self with spell (Health Restore: 10 * lvl)");
        }
        output("[33] Attack with class weapon (Damage: " + (Strength/2) + " (Strength / 2))");
        output("[41] Drink Elixer of Death");
        try {
            validMove = false;
            while(!validMove) {
                input(" -> ");
                int move = Integer.parseInt(scan.nextLine());
                if(move < 0 || move > 4) {
                    proceed = false;
                }
                if(move != 10 && move != 2 && move != 33 && move != 41) {
                    proceed = false;
                }
                if(move == 10 || move == 2 || move == 33 || move == 41) proceed = true;
                if(move == 10 && (classBase.sselcClass == "Mage" || classBase.sselcClass == "Demon")) {
                    Damage = 0;
                    Damage = (15 * classBase.lvl);
                    Damage /= 2;
                    validMove = true;
                } else if(move == 2 && (classBase.sselcClass == "Mage" || classBase.sselcClass == "Priest")) {
                    Damage = 0;
                    Health += (10*classBase.lvl);
                    if(Health > MAX_HEALTH) {
                        Health = MAX_HEALTH;
                    }
                    validMove = true;
                } else if(move == 33) {
                    Damage = Strength / 2;
                    validMove = true;
                } else if(move == 41) {
                    Health = 0;
                    output("You Killed Yourself! (Health: " + Health + " )");
                    System.exit(-1);
                }
            }
        } catch (NumberFormatException e) {
            ERRoutput("Please enter a valid numberical value!");
        }
        if(!proceed) {
            Continue = false;
            while(!proceed) {
                output("What do you do? (Select Number)");
                if(classBase.dmg_Spls == true) {
                    output("[10] Damage enemy with spell (Damage: " + (7.5*classBase.lvl) + ")");
                }
                if(classBase.heal_Spls == true) {
                    output("[2] Heal self with spell (Health Restore: 10 * lvl)");
                }
                output("[33] Attack with class weapon (Damage: " + (Strength/2) + " (Strength / 2))");
                output("[41] Drink Elixer of Death");
                try {
                    validMove = false;
                    while(!validMove) {
                        input(" -> ");
                        int move = Integer.parseInt(scan.nextLine());
                        if(move < 0 || move > 4) {
                            proceed = false;
                        }
                        if(move != 10 && move != 2 && move != 33 && move != 41) {
                            proceed = false;
                        }
                        if(move == 10 || move == 2 || move == 33 || move == 41) proceed = true;
                        if(move == 10 && (classBase.sselcClass == "Mage" || classBase.sselcClass == "Demon")) {
                            Damage = 0;
                            Damage = (15 * classBase.lvl);
                            Damage /= 2;
                            validMove = true;
                        } else if(move == 2 && (classBase.sselcClass == "Mage" || classBase.sselcClass == "Priest")) {
                            Damage = 0;
                            Health += (10 * classBase.lvl);
                            if(Health > MAX_HEALTH) {
                                Health = MAX_HEALTH;
                            }
                        } else if(move == 33) {
                            Damage = Strength / 2;
                        } else if(move == 41) {
                            Health = 0;
                            output("You Killed Yourself! (Health: " + Health + " )");
                            System.exit(-1);
                        }
                    }
                } catch (NumberFormatException e) {
                    ERRoutput("Please enter a valid numberical value!");
                }
            }
        }
        eHealth -= Damage;
        showEnemyHealth();
        int allowed;
        int am = 0;
        if(Stealth != 0) {
            for(int i = 0; i < Stealth/2; i++) {
                am++;
            }
        } else {
            am = 0;
        }
        allowed = r.nextInt(am);
        if(allowed == 0) {
            if(proceed && eHealth > 0) {
                output("Enemy attacked and dealt " + eDamage + " damage!");
                Health -= eDamage;
                if(Health <= 0) {
                    output("You have died! (Health: " + Health + ")");
                    System.exit(-1);
                }
            } else if(eHealth <= 0) {
                output("Enemy defeated!");
                classBase.XP += 5;
                ENEMY = false;
            }
        } else {
            output("The enemy missed it's attack!");
        }
    }
    if(Health == 0) {
        output("You have died! (Health: " + Health + ")");
        System.exit(-1);
    }
}

标签: javaeclipse

解决方案


推荐阅读